diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2017-10-24 14:37:24 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2017-12-04 17:57:56 +0300 |
commit | a9bc9e596e37ad84b50d52e66937dd53d8dea666 (patch) | |
tree | 7e03b28037abb020b3d5c47fbf9bb92806af8f0b | |
parent | 3e1918f099c436f3c1e7411b07b5d0cb639304fb (diff) |
Memoize exists too
-rw-r--r-- | app/models/repository.rb | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 05538d8bbfa..e8213209e0c 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -496,18 +496,23 @@ class Repository def exists? return false unless full_path - value = Gitlab::Redis::Cache.with { |redis| redis.get(exists_cache_key) } - return JSON.parse(value).first unless value.nil? + return @exists if defined(@exists) - new_value = raw_repository.exists? + redis_value = Gitlab::Redis::Cache.with { |redis| redis.get(exists_cache_key) } - # We don't use 'cache_method' because that has a two-week expiry time - # (via Rails.cache). A false negative from 'exists?' has a severe impact - # on user experience. By using a shorter expiry time we limit the impact - # of such a fault. - Gitlab::Redis::Cache.with { |redis| redis.set(exists_cache_key, [new_value].to_json, ex: EXISTS_CACHE_TTL) } + if redis_value.nil? + @exists = raw_repository.exists? - new_value + # We don't use 'cache_method' because that has a two-week expiry time + # (via Rails.cache). A false negative from 'exists?' has a severe impact + # on user experience. By using a shorter expiry time we limit the impact + # of such a fault. + Gitlab::Redis::Cache.with { |redis| redis.set(exists_cache_key, [@exists].to_json, ex: EXISTS_CACHE_TTL) } + else + @exists = JSON.parse(redis_value).first + end + + @exists end def exists_cache_key |