Refactor RedisCacheStoreProxy to unlearn everything about redis client details to make it less prone to bugs in the future

Let RedisCacheStoreProxy only know and assume things about
RedisCacheStore API. Don't let it know anything about the specific redis
client behind the scenes, that's the job of RedisCacheStore only, not
ours.
This commit is contained in:
Gonzalo Rodriguez 2018-06-20 18:32:55 -03:00
parent 3caee5c3ca
commit 3af7394b6a
No known key found for this signature in database
GPG key ID: 5DB8B81B049B8AB1

View file

@ -9,13 +9,16 @@ module Rack
end
def increment(name, amount, options = {})
# Redis doesn't check expiration on the INCRBY command. See https://redis.io/commands/expire
redis.with do |r|
count = r.pipelined do
r.incrby(name, amount)
r.expire(name, options[:expires_in]) if options[:expires_in]
end
count.first
# RedisCacheStore#increment ignores options[:expires_in].
#
# So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
# the counter. After that we continue using the original RedisCacheStore#increment.
if options[:expires_in] && !read(name)
write(name, 1, options)
1
else
super
end
end