mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Avoid as much repetition as possible between RedisProxy and RedisStoreProxy
This commit is contained in:
parent
b40b5718dc
commit
673cf98157
2 changed files with 13 additions and 30 deletions
|
|
@ -6,16 +6,21 @@ module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class RedisProxy < SimpleDelegator
|
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)
|
def self.handle?(store)
|
||||||
defined?(::Redis) && store.is_a?(::Redis)
|
defined?(::Redis) && store.is_a?(::Redis)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(store)
|
|
||||||
super(store)
|
|
||||||
end
|
|
||||||
|
|
||||||
def read(key)
|
def read(key)
|
||||||
get(key)
|
get(key)
|
||||||
|
rescue Redis::BaseError
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(key, value, options = {})
|
def write(key, value, options = {})
|
||||||
|
|
@ -24,6 +29,7 @@ module Rack
|
||||||
else
|
else
|
||||||
set(key, value)
|
set(key, value)
|
||||||
end
|
end
|
||||||
|
rescue Redis::BaseError
|
||||||
end
|
end
|
||||||
|
|
||||||
def increment(key, amount, options = {})
|
def increment(key, amount, options = {})
|
||||||
|
|
@ -35,10 +41,12 @@ module Rack
|
||||||
end
|
end
|
||||||
|
|
||||||
count.value if count
|
count.value if count
|
||||||
|
rescue Redis::BaseError
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(key, _options = {})
|
def delete(key, _options = {})
|
||||||
del(key)
|
del(key)
|
||||||
|
rescue Redis::BaseError
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,7 @@ require 'delegate'
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
class RedisStoreProxy < SimpleDelegator
|
class RedisStoreProxy < RedisProxy
|
||||||
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)
|
def self.handle?(store)
|
||||||
defined?(::Redis::Store) && store.is_a?(::Redis::Store)
|
defined?(::Redis::Store) && store.is_a?(::Redis::Store)
|
||||||
end
|
end
|
||||||
|
|
@ -31,23 +23,6 @@ module Rack
|
||||||
end
|
end
|
||||||
rescue Redis::BaseError
|
rescue Redis::BaseError
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue