summaryrefslogtreecommitdiff
path: root/app/services/projects/forks_count_service.rb
blob: e2e2b1da91d66fcaeb9d8af48fa8e4be7de116e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Projects
  # Service class for getting and caching the number of forks of a project.
  class ForksCountService
    def initialize(project)
      @project = project
    end

    def count
      Rails.cache.fetch(cache_key) { uncached_count }
    end

    def refresh_cache
      Rails.cache.write(cache_key, uncached_count)
    end

    def delete_cache
      Rails.cache.delete(cache_key)
    end

    private

    def uncached_count
      @project.forks.count
    end

    def cache_key
      ['projects', @project.id, 'forks_count']
    end
  end
end