Class: Gitlab::RepositoryCache

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/repository_cache.rb,
lib/gitlab/repository_cache/preloader.rb

Defined Under Namespace

Classes: Preloader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, extra_namespace: nil, backend: Rails.cache) ⇒ RepositoryCache

Returns a new instance of RepositoryCache.


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

def initialize(repository, extra_namespace: nil, backend: Rails.cache)
  @repository = repository
  @namespace = "#{repository.full_path}"
  @namespace += ":#{repository.project.id}" if repository.project
  @namespace = "#{@namespace}:#{extra_namespace}" if extra_namespace
  @backend = backend
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.


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

def backend
  @backend
end

#namespaceObject (readonly)

Returns the value of attribute namespace.


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

def namespace
  @namespace
end

#repositoryObject (readonly)

Returns the value of attribute repository.


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

def repository
  @repository
end

Instance Method Details

#cache_key(type) ⇒ Object


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

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

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
# File 'lib/gitlab/repository_cache.rb', line 28

def exist?(key)
  backend.exist?(cache_key(key))
end

#expire(key) ⇒ Object


20
21
22
# File 'lib/gitlab/repository_cache.rb', line 20

def expire(key)
  backend.delete(cache_key(key))
end

#fetch(key, &block) ⇒ Object


24
25
26
# File 'lib/gitlab/repository_cache.rb', line 24

def fetch(key, &block)
  backend.fetch(cache_key(key), &block)
end

#fetch_without_caching_false(key, &block) ⇒ Object


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

def fetch_without_caching_false(key, &block)
  value = read(key)
  return value if value

  value = yield

  # Don't cache false values
  write(key, value) if value

  value
end

#read(key) ⇒ Object


32
33
34
# File 'lib/gitlab/repository_cache.rb', line 32

def read(key)
  backend.read(cache_key(key))
end

#write(key, value, *args) ⇒ Object


36
37
38
# File 'lib/gitlab/repository_cache.rb', line 36

def write(key, value, *args)
  backend.write(cache_key(key), value, *args)
end