diff options
Diffstat (limited to 'app/services/protected_branches/cache_service.rb')
-rw-r--r-- | app/services/protected_branches/cache_service.rb | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/app/services/protected_branches/cache_service.rb b/app/services/protected_branches/cache_service.rb index 8c521f4ebcb..66ca549c508 100644 --- a/app/services/protected_branches/cache_service.rb +++ b/app/services/protected_branches/cache_service.rb @@ -7,20 +7,26 @@ module ProtectedBranches CACHE_EXPIRE_IN = 1.day CACHE_LIMIT = 1000 - def fetch(ref_name, dry_run: false) + def fetch(ref_name, dry_run: false, &block) record = OpenSSL::Digest::SHA256.hexdigest(ref_name) - Gitlab::Redis::Cache.with do |redis| + with_redis do |redis| cached_result = redis.hget(redis_key, record) - decoded_result = Gitlab::Redis::Boolean.decode(cached_result) unless cached_result.nil? + if cached_result.nil? + metrics.increment_cache_miss + else + metrics.increment_cache_hit + + decoded_result = Gitlab::Redis::Boolean.decode(cached_result) + end # If we're dry-running, don't break because we need to check against # the real value to ensure the cache is working properly. # If the result is nil we'll need to run the block, so don't break yet. break decoded_result unless dry_run || decoded_result.nil? - calculated_value = yield + calculated_value = metrics.observe_cache_generation(&block) check_and_log_discrepancy(decoded_result, calculated_value, ref_name) if dry_run @@ -42,11 +48,15 @@ module ProtectedBranches end def refresh - Gitlab::Redis::Cache.with { |redis| redis.unlink(redis_key) } + with_redis { |redis| redis.unlink(redis_key) } end private + def with_redis(&block) + Gitlab::Redis::Cache.with(&block) # rubocop:disable CodeReuse/ActiveRecord + end + def check_and_log_discrepancy(cached_value, real_value, ref_name) return if cached_value.nil? return if cached_value == real_value @@ -64,5 +74,14 @@ module ProtectedBranches def redis_key @redis_key ||= [CACHE_ROOT_KEY, @project.id].join(':') end + + def metrics + @metrics ||= Gitlab::Cache::Metrics.new( + caller_id: Gitlab::ApplicationContext.current_context_attribute(:caller_id), + cache_identifier: "#{self.class}#fetch", + feature_category: :source_code_management, + backing_resource: :cpu + ) + end end end |