2020-06-24 11:08:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
#
|
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class StatsCache
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
EXPIRATION = 1.week
|
2020-08-03 23:09:50 -04:00
|
|
|
# The DiffStats#as_json representation is tied to the Gitaly protobuf version
|
|
|
|
VERSION = Gem.loaded_specs['gitaly'].version.to_s
|
2020-06-24 11:08:50 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-08-05 14:09:49 -04:00
|
|
|
cache.write(key, stats.map(&:to_h).as_json, expires_in: EXPIRATION)
|
2020-08-03 23:09:50 -04:00
|
|
|
clear_memoization(:cached_values)
|
2020-06-24 11:08:50 -04:00
|
|
|
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
|