2017-02-07 17:06:08 +00:00
|
|
|
module Gitlab
|
|
|
|
module EtagCaching
|
|
|
|
class Store
|
2017-03-23 11:23:13 +00:00
|
|
|
EXPIRY_TIME = 20.minutes
|
2017-02-07 17:06:08 +00:00
|
|
|
REDIS_NAMESPACE = 'etag:'.freeze
|
|
|
|
|
|
|
|
def get(key)
|
|
|
|
Gitlab::Redis.with { |redis| redis.get(redis_key(key)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def touch(key, only_if_missing: false)
|
|
|
|
etag = generate_etag
|
|
|
|
|
|
|
|
Gitlab::Redis.with do |redis|
|
|
|
|
redis.set(redis_key(key), etag, ex: EXPIRY_TIME, nx: only_if_missing)
|
|
|
|
end
|
|
|
|
|
|
|
|
etag
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_etag
|
|
|
|
SecureRandom.hex
|
|
|
|
end
|
|
|
|
|
|
|
|
def redis_key(key)
|
|
|
|
"#{REDIS_NAMESPACE}#{key}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|