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
-
#cache ⇒ Object
RepositoryCache to be used.
-
#cache_method_output(name, fallback: nil, &block) ⇒ Object
Caches and strongly memoizes the supplied block.
-
#cache_method_output_as_redis_set(name, fallback: nil, &block) ⇒ Object
Caches and strongly memoizes the supplied block as a Redis Set.
-
#cache_method_output_asymmetrically(name, &block) ⇒ Object
Caches truthy values from the supplied block.
-
#cached_methods ⇒ Object
List of cached methods.
-
#expire_method_caches(methods) ⇒ Object
Expires the caches of a specific set of methods.
- #memoize_method_cache_value(method, value) ⇒ Object
-
#memoize_method_output(name, fallback: nil, &block) ⇒ Object
Strongly memoizes the supplied block.
-
#no_repository_fallback(name, fallback: nil, &block) ⇒ Object
Returns the fallback value if the repository does not exist.
-
#redis_hash_cache ⇒ Object
RepositoryHashCache to be used.
-
#redis_set_cache ⇒ Object
RepositorySetCache to be used.
-
#request_store_cache ⇒ Object
RequestStore-backed RepositoryCache to be used.
Instance Method Details
#cache ⇒ Object
RepositoryCache to be used. Should be overridden by the including class
149 150 151 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 149 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.
173 174 175 176 177 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 173 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.
185 186 187 188 189 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 185 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.
199 200 201 202 203 204 205 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 199 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_methods ⇒ Object
List of cached methods. Should be overridden by the including class
164 165 166 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 164 def cached_methods raise NotImplementedError end |
#expire_method_caches(methods) ⇒ Object
Expires the caches of a specific set of methods
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 237 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
232 233 234 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 232 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.
213 214 215 216 217 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 213 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
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 220 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_cache ⇒ Object
RepositoryHashCache to be used. Should be overridden by the including class
159 160 161 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 159 def redis_hash_cache raise NotImplementedError end |
#redis_set_cache ⇒ Object
RepositorySetCache to be used. Should be overridden by the including class
154 155 156 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 154 def redis_set_cache raise NotImplementedError end |
#request_store_cache ⇒ Object
RequestStore-backed RepositoryCache to be used. Should be overridden by the including class
144 145 146 |
# File 'lib/gitlab/repository_cache_adapter.rb', line 144 def request_store_cache raise NotImplementedError end |