Class: Gitlab::RepositorySetCache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SetCache

#exist?, #expire, #include?, #read, #try_include?, #ttl

Constructor Details

#initialize(repository, extra_namespace: nil, expires_in: 2.weeks) ⇒ RepositorySetCache

Returns a new instance of RepositorySetCache.


8
9
10
11
12
13
14
# File 'lib/gitlab/repository_set_cache.rb', line 8

def initialize(repository, extra_namespace: nil, expires_in: 2.weeks)
  @repository = repository
  @namespace = "#{repository.full_path}"
  @namespace += ":#{repository.project.id}" if repository.project
  @namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
  @expires_in = expires_in
end

Instance Attribute Details

#expires_inObject (readonly)

Returns the value of attribute expires_in.


6
7
8
# File 'lib/gitlab/repository_set_cache.rb', line 6

def expires_in
  @expires_in
end

#namespaceObject (readonly)

Returns the value of attribute namespace.


6
7
8
# File 'lib/gitlab/repository_set_cache.rb', line 6

def namespace
  @namespace
end

#repositoryObject (readonly)

Returns the value of attribute repository.


6
7
8
# File 'lib/gitlab/repository_set_cache.rb', line 6

def repository
  @repository
end

Instance Method Details

#cache_key(type) ⇒ Object


16
17
18
# File 'lib/gitlab/repository_set_cache.rb', line 16

def cache_key(type)
  super("#{type}:#{namespace}")
end

#fetch(key, &block) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/repository_set_cache.rb', line 38

def fetch(key, &block)
  full_key = cache_key(key)

  smembers, exists = with do |redis|
    redis.multi do
      redis.smembers(full_key)
      redis.exists(full_key)
    end
  end

  return smembers if exists

  write(key, yield)
end

#search(key, pattern, &block) ⇒ Object

Searches the cache set using SSCAN with the MATCH option. The MATCH parameter is the pattern argument. See redis.io/commands/scan#the-match-option for more information. Returns an Enumerator that enumerates all SSCAN hits.


57
58
59
60
61
62
63
64
65
66
# File 'lib/gitlab/repository_set_cache.rb', line 57

def search(key, pattern, &block)
  full_key = cache_key(key)

  with do |redis|
    exists = redis.exists(full_key)
    write(key, yield) unless exists

    redis.sscan_each(full_key, match: pattern)
  end
end

#write(key, value) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/repository_set_cache.rb', line 20

def write(key, value)
  full_key = cache_key(key)

  with do |redis|
    redis.multi do
      redis.unlink(full_key)

      # Splitting into groups of 1000 prevents us from creating a too-long
      # Redis command
      value.each_slice(1000) { |subset| redis.sadd(full_key, subset) }

      redis.expire(full_key, expires_in)
    end
  end

  value
end