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

Instance Method Details

#cacheObject

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

Raises:

  • (NotImplementedError)


137
138
139
# File 'lib/gitlab/repository_cache_adapter.rb', line 137

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.


161
162
163
164
165
# File 'lib/gitlab/repository_cache_adapter.rb', line 161

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.


173
174
175
176
177
# File 'lib/gitlab/repository_cache_adapter.rb', line 173

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.



187
188
189
190
191
192
193
# File 'lib/gitlab/repository_cache_adapter.rb', line 187

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)


152
153
154
# File 'lib/gitlab/repository_cache_adapter.rb', line 152

def cached_methods
  raise NotImplementedError
end

#expire_method_caches(methods) ⇒ Object

Expires the caches of a specific set of methods



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gitlab/repository_cache_adapter.rb', line 225

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_cache_value(method, value) ⇒ Object



220
221
222
# File 'lib/gitlab/repository_cache_adapter.rb', line 220

def memoize_method_cache_value(method, value)
  strong_memoize(memoizable_name(method)) { value }
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.


201
202
203
204
205
# File 'lib/gitlab/repository_cache_adapter.rb', line 201

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



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/gitlab/repository_cache_adapter.rb', line 208

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)


147
148
149
# File 'lib/gitlab/repository_cache_adapter.rb', line 147

def redis_hash_cache
  raise NotImplementedError
end

#redis_set_cacheObject

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

Raises:

  • (NotImplementedError)


142
143
144
# File 'lib/gitlab/repository_cache_adapter.rb', line 142

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)


132
133
134
# File 'lib/gitlab/repository_cache_adapter.rb', line 132

def request_store_cache
  raise NotImplementedError
end