Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stats_cache.rb « diff « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f38fb21d4972236f90cf62551e75fc47a6461925 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true
#
module Gitlab
  module Diff
    class StatsCache
      include Gitlab::Metrics::Methods
      include Gitlab::Utils::StrongMemoize

      EXPIRATION = 1.week
      VERSION = 1

      def initialize(cachable_key:)
        @cachable_key = cachable_key
      end

      def read
        strong_memoize(:cached_values) do
          content = cache.fetch(key)

          next unless content

          stats = content.map { |stat| Gitaly::DiffStats.new(stat) }

          Gitlab::Git::DiffStatsCollection.new(stats)
        end
      end

      def write_if_empty(stats)
        return if cache.exist?(key)
        return unless stats

        cache.write(key, stats.as_json, expires_in: EXPIRATION)
      end

      def clear
        cache.delete(key)
      end

      private

      attr_reader :cachable_key

      def cache
        Rails.cache
      end

      def key
        strong_memoize(:redis_key) do
          ['diff_stats', cachable_key, VERSION].join(":")
        end
      end
    end
  end
end