Class: Gitlab::AvatarCache

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/avatar_cache.rb

Constant Summary collapse

VERSION =

Increment this if a breaking change requires immediate cache expiry of all avatar caches.

Returns:

  • (Integer)
2
BASE_KEY =

Returns:

  • (Symbol)
:avatar_cache
DEFAULT_EXPIRY =

Returns:

  • (ActiveSupport::Duration)
7.days

Class Method Summary collapse

Class Method Details

.by_email(email, *additional_keys, expires_in: DEFAULT_EXPIRY) {|email, *additional_keys| ... } ⇒ String

Look up cached avatar data by email address. This accepts a block to provide the value to be cached in the event nothing is found.

Multiple calls in the same request will be served from the request store.

Parameters:

  • email (String)
  • additional_keys (*Object)

    all must respond to ‘#to_s`

  • expires_in (ActiveSupport::Duration, Integer) (defaults to: DEFAULT_EXPIRY)

Yields:

  • (email, *additional_keys)

    yields the supplied params back to the block

Returns:

  • (String)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/avatar_cache.rb', line 30

def by_email(email, *additional_keys, expires_in: DEFAULT_EXPIRY)
  key = email_key(email)
  subkey = additional_keys.join(":")

  Gitlab::SafeRequestStore.fetch([key, subkey]) do
    with do |redis|
      # Look for existing cache value
      cached = redis.hget(key, subkey)

      # Return the cached entry if set
      break cached unless cached.nil?

      # Otherwise, call the block to get the value
      to_cache = yield(email, *additional_keys).to_s

      # Set it in the cache
      redis.hset(key, subkey, to_cache)

      # Update the expiry time
      redis.expire(key, expires_in)

      # Return this new value
      break to_cache
    end
  end
end

.delete_by_email(*emails) ⇒ Integer

Remove one or more emails from the cache

Parameters:

  • emails (String)

    one or more emails to delete

Returns:

  • (Integer)

    the number of keys deleted



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitlab/avatar_cache.rb', line 61

def delete_by_email(*emails)
  return 0 if emails.empty?

  with do |redis|
    keys = emails.map { |email| email_key(email) }

    Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do
      if Gitlab::Redis::ClusterUtil.cluster?(redis)
        Gitlab::Redis::ClusterUtil.batch_unlink(keys, redis)
      else
        redis.unlink(*keys)
      end
    end
  end
end