Class: ProtectedBranches::CacheService

Inherits:
BaseService show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/protected_branches/cache_service.rb

Constant Summary collapse

TTL_UNSET =
-1
CACHE_EXPIRE_IN =
1.day
CACHE_LIMIT =
1000

Instance Attribute Summary

Attributes inherited from BaseService

#project_or_group

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#after_execute, #initialize, #refresh_cache

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

This class inherits a constructor from ProtectedBranches::BaseService

Instance Method Details

#fetch(ref_name, dry_run: false, &block) ⇒ Object



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
# File 'app/services/protected_branches/cache_service.rb', line 11

def fetch(ref_name, dry_run: false, &block)
  record = OpenSSL::Digest::SHA256.hexdigest(ref_name)

  with_redis do |redis|
    cached_result = redis.hget(redis_key, record)

    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 = metrics.observe_cache_generation(&block)

    check_and_log_discrepancy(decoded_result, calculated_value, ref_name) if dry_run

    redis.hset(redis_key, record, Gitlab::Redis::Boolean.encode(calculated_value))

    # We don't want to extend cache expiration time
    if redis.ttl(redis_key) == TTL_UNSET
      redis.expire(redis_key, CACHE_EXPIRE_IN)
    end

    # If the cache record has too many elements, then something went wrong and
    # it's better to drop the cache key.
    if redis.hlen(redis_key) > CACHE_LIMIT
      redis.unlink(redis_key)
    end

    calculated_value
  end
end

#refreshObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/services/protected_branches/cache_service.rb', line 51

def refresh
  with_redis { |redis| redis.unlink(redis_key) }

  return unless (group = project_or_group).is_a?(Group)

  group.all_projects.each_batch do |projects_relation|
    # First we remove the cache for each project in the group and then
    # touch the projects_relation to update the projects' cache key.
    with_redis do |redis|
      projects_relation.each do |project|
        redis.unlink redis_key(project)
      end
    end
    projects_relation.touch_all
  end
end