Class: Gitlab::Diff::StatsCache

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/diff/stats_cache.rb

Constant Summary collapse

EXPIRATION =
1.week
VERSION =

The DiffStats#as_json representation is tied to the Gitaly protobuf version

Gem.loaded_specs['gitaly'].version.to_s

Instance Method Summary collapse

Constructor Details

#initialize(cachable_key:) ⇒ StatsCache

Returns a new instance of StatsCache.



12
13
14
# File 'lib/gitlab/diff/stats_cache.rb', line 12

def initialize(cachable_key:)
  @cachable_key = cachable_key
end

Instance Method Details

#clearObject



36
37
38
# File 'lib/gitlab/diff/stats_cache.rb', line 36

def clear
  cache.delete(key)
end

#readObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/diff/stats_cache.rb', line 16

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

#write_if_empty(stats) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/gitlab/diff/stats_cache.rb', line 28

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

  cache.write(key, stats.map(&:to_h).as_json, expires_in: EXPIRATION)
  clear_memoization(:cached_values)
end