diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /lib/gitlab/diff/stats_cache.rb | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'lib/gitlab/diff/stats_cache.rb')
-rw-r--r-- | lib/gitlab/diff/stats_cache.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/gitlab/diff/stats_cache.rb b/lib/gitlab/diff/stats_cache.rb new file mode 100644 index 00000000000..f38fb21d497 --- /dev/null +++ b/lib/gitlab/diff/stats_cache.rb @@ -0,0 +1,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 |