mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-26 14:57:47 +00:00
feature: support for ActiveSupport::MemCacheStore
This commit is contained in:
parent
6789b28c7b
commit
397a7ce7b4
3 changed files with 6 additions and 3 deletions
|
|
@ -6,14 +6,14 @@ module Rack
|
||||||
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
|
||||||
if defined?(::ActiveSupport::Cache::RedisStore) && store.is_a?(::ActiveSupport::Cache::RedisStore)
|
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,
|
# ActiveSupport::Cache::RedisStore doesn't expose any way to set an expiry,
|
||||||
# so use the raw Redis::Store instead
|
# so use the raw Redis::Store instead
|
||||||
store = store.instance_variable_get(:@data)
|
store = store.instance_variable_get(:@data)
|
||||||
end
|
end
|
||||||
|
|
||||||
klass = PROXIES.find { |proxy| proxy.handle?(store) }
|
klass = PROXIES.find { |proxy| proxy.handle?(store) }
|
||||||
|
|
||||||
klass ? klass.new(store) : store
|
klass ? klass.new(store) : store
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ module Rack
|
||||||
if defined?(::ConnectionPool) && store.is_a?(::ConnectionPool)
|
if defined?(::ConnectionPool) && store.is_a?(::ConnectionPool)
|
||||||
store.with { |conn| conn.is_a?(::Dalli::Client) }
|
store.with { |conn| conn.is_a?(::Dalli::Client) }
|
||||||
else
|
else
|
||||||
store.is_a?(::Dalli::Client)
|
store.is_a?(::Dalli::Client) || store.is_a?(::ActiveSupport::Cache::MemCacheStore)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,14 @@ describe Rack::Attack::Cache do
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'active_support/cache/dalli_store'
|
require 'active_support/cache/dalli_store'
|
||||||
|
require 'active_support/cache/mem_cache_store'
|
||||||
require 'active_support/cache/redis_store'
|
require 'active_support/cache/redis_store'
|
||||||
require 'connection_pool'
|
require 'connection_pool'
|
||||||
cache_stores = [
|
cache_stores = [
|
||||||
ActiveSupport::Cache::MemoryStore.new,
|
ActiveSupport::Cache::MemoryStore.new,
|
||||||
ActiveSupport::Cache::DalliStore.new("127.0.0.1"),
|
ActiveSupport::Cache::DalliStore.new("127.0.0.1"),
|
||||||
ActiveSupport::Cache::RedisStore.new("127.0.0.1"),
|
ActiveSupport::Cache::RedisStore.new("127.0.0.1"),
|
||||||
|
ActiveSupport::Cache::MemCacheStore.new("127.0.0.1"),
|
||||||
Dalli::Client.new,
|
Dalli::Client.new,
|
||||||
ConnectionPool.new { Dalli::Client.new },
|
ConnectionPool.new { Dalli::Client.new },
|
||||||
Redis::Store.new
|
Redis::Store.new
|
||||||
|
|
@ -54,6 +56,7 @@ describe Rack::Attack::Cache do
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "do_count after expires_in" do
|
describe "do_count after expires_in" do
|
||||||
it "must be 1" do
|
it "must be 1" do
|
||||||
@cache.send(:do_count, @key, @expires_in)
|
@cache.send(:do_count, @key, @expires_in)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue