diff --git a/lib/rack/attack/store_proxy/redis_proxy.rb b/lib/rack/attack/store_proxy/redis_proxy.rb index 756c4c6..69fa19d 100644 --- a/lib/rack/attack/store_proxy/redis_proxy.rb +++ b/lib/rack/attack/store_proxy/redis_proxy.rb @@ -6,16 +6,21 @@ module Rack class Attack module StoreProxy class RedisProxy < SimpleDelegator + def initialize(*args) + if Gem::Version.new(Redis::VERSION) < Gem::Version.new("3") + warn 'RackAttack requires Redis gem >= 3.0.0.' + end + + super(*args) + end + def self.handle?(store) defined?(::Redis) && store.is_a?(::Redis) end - def initialize(store) - super(store) - end - def read(key) get(key) + rescue Redis::BaseError end def write(key, value, options = {}) @@ -24,6 +29,7 @@ module Rack else set(key, value) end + rescue Redis::BaseError end def increment(key, amount, options = {}) @@ -35,10 +41,12 @@ module Rack end count.value if count + rescue Redis::BaseError end def delete(key, _options = {}) del(key) + rescue Redis::BaseError end end end diff --git a/lib/rack/attack/store_proxy/redis_store_proxy.rb b/lib/rack/attack/store_proxy/redis_store_proxy.rb index a7ac81a..359b542 100644 --- a/lib/rack/attack/store_proxy/redis_store_proxy.rb +++ b/lib/rack/attack/store_proxy/redis_store_proxy.rb @@ -5,15 +5,7 @@ require 'delegate' module Rack class Attack module StoreProxy - class RedisStoreProxy < SimpleDelegator - def initialize(*args) - if Gem::Version.new(Redis::VERSION) < Gem::Version.new("3") - warn 'RackAttack requires Redis gem >= 3.0.0.' - end - - super(*args) - end - + class RedisStoreProxy < RedisProxy def self.handle?(store) defined?(::Redis::Store) && store.is_a?(::Redis::Store) end @@ -31,23 +23,6 @@ module Rack end rescue Redis::BaseError end - - def increment(key, amount, options = {}) - count = nil - - pipelined do - count = incrby(key, amount) - expire(key, options[:expires_in]) if options[:expires_in] - end - - count.value if count - rescue Redis::BaseError - end - - def delete(key, _options = {}) - del(key) - rescue Redis::BaseError - end end end end