Module: Gitlab::RepositoryCacheAdapter

Extended by:
ActiveSupport::Concern
Includes:
Utils::StrongMemoize
Included in:
Conflict::FileCollection, Repository
Defined in:
lib/gitlab/repository_cache_adapter.rb

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Instance Method Details

#cacheObject

RepositoryCache to be used. Should be overridden by the including class

Raises:

  • (NotImplementedError)

134
135
136
# File 'lib/gitlab/repository_cache_adapter.rb', line 134

def cache
  raise NotImplementedError
end

#cache_method_output(name, fallback: nil, &block) ⇒ Object

Caches and strongly memoizes the supplied block.

name - The name of the method to be cached. fallback - A value to fall back to if the repository does not exist, or

in case of a Git error. Defaults to nil.

158
159
160
161
162
# File 'lib/gitlab/repository_cache_adapter.rb', line 158

def cache_method_output(name, fallback: nil, &block)
  memoize_method_output(name, fallback: fallback) do
    cache.fetch(name, &block)
  end
end

#cache_method_output_as_redis_set(name, fallback: nil, &block) ⇒ Object

Caches and strongly memoizes the supplied block as a Redis Set. The result will be provided as a sorted array.

name - The name of the method to be cached. fallback - A value to fall back to if the repository does not exist, or

in case of a Git error. Defaults to nil.

170
171
172
173
174
# File 'lib/gitlab/repository_cache_adapter.rb', line 170

def cache_method_output_as_redis_set(name, fallback: nil, &block)
  memoize_method_output(name, fallback: fallback) do
    redis_set_cache.fetch(name, &block).sort
  end
end

#cache_method_output_asymmetrically(name, &block) ⇒ Object

Caches truthy values from the supplied block. All values are strongly memoized, and cached in RequestStore.

Currently only used to cache `exists?` since stale false values are particularly troublesome. This can occur, for example, when an NFS mount is temporarily down.

name - The name of the method to be cached.


184
185
186
187
188
189
190
# File 'lib/gitlab/repository_cache_adapter.rb', line 184

def cache_method_output_asymmetrically(name, &block)
  memoize_method_output(name) do
    request_store_cache.fetch(name) do
      cache.fetch_without_caching_false(name, &block)
    end
  end
end

#cached_methodsObject

List of cached methods. Should be overridden by the including class

Raises:

  • (NotImplementedError)

149
150
151
# File 'lib/gitlab/repository_cache_adapter.rb', line 149

def cached_methods
  raise NotImplementedError
end

#expire_method_caches(methods) ⇒ Object

Expires the caches of a specific set of methods


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/gitlab/repository_cache_adapter.rb', line 218

def expire_method_caches(methods)
  methods.each do |name|
    unless cached_methods.include?(name.to_sym)
      Gitlab::AppLogger.error "Requested to expire non-existent method '#{name}' for Repository"
      next
    end

    cache.expire(name)

    clear_memoization(memoizable_name(name))
  end

  expire_redis_set_method_caches(methods)
  expire_redis_hash_method_caches(methods)
  expire_request_store_method_caches(methods)
end

#memoize_method_output(name, fallback: nil, &block) ⇒ Object

Strongly memoizes the supplied block.

name - The name of the method to be memoized. fallback - A value to fall back to if the repository does not exist, or

in case of a Git error. Defaults to nil. The fallback value is
not memoized.

198
199
200
201
202
# File 'lib/gitlab/repository_cache_adapter.rb', line 198

def memoize_method_output(name, fallback: nil, &block)
  no_repository_fallback(name, fallback: fallback) do
    strong_memoize(memoizable_name(name), &block)
  end
end

#no_repository_fallback(name, fallback: nil, &block) ⇒ Object

Returns the fallback value if the repository does not exist


205
206
207
208
209
210
211
212
213
214
215
# File 'lib/gitlab/repository_cache_adapter.rb', line 205

def no_repository_fallback(name, fallback: nil, &block)
  # Avoid unnecessary gRPC invocations
  return fallback if fallback && fallback_early?(name)

  yield
rescue Gitlab::Git::Repository::NoRepository
  # Even if the `#exists?` check in `fallback_early?` passes, these errors
  # might still occur (for example because of a non-existing HEAD). We
  # want to gracefully handle this and not memoize anything.
  fallback
end

#redis_hash_cacheObject

RepositoryHashCache to be used. Should be overridden by the including class

Raises:

  • (NotImplementedError)

144
145
146
# File 'lib/gitlab/repository_cache_adapter.rb', line 144

def redis_hash_cache
  raise NotImplementedError
end

#redis_set_cacheObject

RepositorySetCache to be used. Should be overridden by the including class

Raises:

  • (NotImplementedError)

139
140
141
# File 'lib/gitlab/repository_cache_adapter.rb', line 139

def redis_set_cache
  raise NotImplementedError
end

#request_store_cacheObject

RequestStore-backed RepositoryCache to be used. Should be overridden by the including class

Raises:

  • (NotImplementedError)

129
130
131
# File 'lib/gitlab/repository_cache_adapter.rb', line 129

def request_store_cache
  raise NotImplementedError
end