mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-24 14:37:39 +00:00
Revert "refactor unwieldy Rack::Attack::StoreProxy.build method"
This reverts commit 8d124d868e.
This commit is contained in:
parent
c34bace773
commit
585d1fd02c
1 changed files with 13 additions and 18 deletions
|
|
@ -2,30 +2,25 @@ module Rack
|
||||||
class Attack
|
class Attack
|
||||||
module StoreProxy
|
module StoreProxy
|
||||||
PROXIES = [DalliProxy, MemCacheProxy, RedisStoreProxy]
|
PROXIES = [DalliProxy, MemCacheProxy, RedisStoreProxy]
|
||||||
USE_BASE_CLIENT = ['Redis::Store', 'Dalli::Client', 'MemCache']
|
|
||||||
|
|
||||||
def self.build(store)
|
def self.build(store)
|
||||||
# RedisStore#increment needs different behavior, so detect that
|
# RedisStore#increment needs different behavior, so detect that
|
||||||
# (method has an arity of 2; must call #expire separately
|
# (method has an arity of 2; must call #expire separately
|
||||||
client = fetch_client(store)
|
if (defined?(::ActiveSupport::Cache::RedisStore) && store.is_a?(::ActiveSupport::Cache::RedisStore)) ||
|
||||||
klass = PROXIES.find { |proxy| proxy.handle?(client) }
|
(defined?(::ActiveSupport::Cache::MemCacheStore) && store.is_a?(::ActiveSupport::Cache::MemCacheStore))
|
||||||
klass ? klass.new(client) : client
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.fetch_client(store)
|
# ActiveSupport::Cache::RedisStore doesn't expose any way to set an expiry,
|
||||||
client = store.instance_variable_get(:@data)
|
# so use the raw Redis::Store instead.
|
||||||
# RedisStore#increment needs different behavior, so detect that
|
# We also want to use the underlying Dalli client instead of ::ActiveSupport::Cache::MemCacheStore,
|
||||||
# (method has an arity of 2; must call #expire separately
|
# and the MemCache client if using Rails 3.x
|
||||||
#
|
client = store.instance_variable_get(:@data)
|
||||||
# ActiveSupport::Cache::RedisStore doesn't expose any way to set an expiry,
|
if (defined?(::Redis::Store) && client.is_a?(Redis::Store)) ||
|
||||||
# so use the raw Redis::Store instead.
|
(defined?(Dalli::Client) && client.is_a?(Dalli::Client)) || (defined?(MemCache) && client.is_a?(MemCache))
|
||||||
#
|
store = store.instance_variable_get(:@data)
|
||||||
# We also want to use the underlying Dalli client instead of ::ActiveSupport::Cache::MemCacheStore,
|
end
|
||||||
# and the MemCache client if using Rails 3.x
|
|
||||||
USE_BASE_CLIENT.each do |klass|
|
|
||||||
return client if !client.nil? && Object.const_defined?(klass) && client.is_a?(Object.const_get(klass))
|
|
||||||
end
|
end
|
||||||
return store
|
klass = PROXIES.find { |proxy| proxy.handle?(store) }
|
||||||
|
klass ? klass.new(store) : store
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue