mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
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:
parent
3caee5c3ca
commit
3af7394b6a
1 changed files with 10 additions and 7 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue