mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
22 lines
821 B
Ruby
22 lines
821 B
Ruby
module Rack
|
|
class Attack
|
|
module StoreProxy
|
|
PROXIES = [DalliProxy, RedisStoreProxy]
|
|
|
|
def self.build(store)
|
|
# RedisStore#increment needs different behavior, so detect that
|
|
# (method has an arity of 2; must call #expire separately
|
|
if (defined?(::ActiveSupport::Cache::RedisStore) && store.is_a?(::ActiveSupport::Cache::RedisStore)) ||
|
|
(defined?(::ActiveSupport::Cache::MemCacheStore) && store.is_a?(::ActiveSupport::Cache::MemCacheStore))
|
|
# ActiveSupport::Cache::RedisStore doesn't expose any way to set an expiry,
|
|
# so use the raw Redis::Store instead
|
|
store = store.instance_variable_get(:@data)
|
|
end
|
|
|
|
klass = PROXIES.find { |proxy| proxy.handle?(store) }
|
|
klass ? klass.new(store) : store
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|