From 397a7ce7b49c95ddf899e1f96b3803797c4e0492 Mon Sep 17 00:00:00 2001 From: Vincent Boisard Date: Tue, 8 Dec 2015 10:53:53 +0100 Subject: [PATCH] feature: support for ActiveSupport::MemCacheStore --- lib/rack/attack/store_proxy.rb | 4 ++-- lib/rack/attack/store_proxy/dalli_proxy.rb | 2 +- spec/integration/rack_attack_cache_spec.rb | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/rack/attack/store_proxy.rb b/lib/rack/attack/store_proxy.rb index 5c476ff..94fb169 100644 --- a/lib/rack/attack/store_proxy.rb +++ b/lib/rack/attack/store_proxy.rb @@ -6,14 +6,14 @@ module Rack 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) + 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 diff --git a/lib/rack/attack/store_proxy/dalli_proxy.rb b/lib/rack/attack/store_proxy/dalli_proxy.rb index 703f2d6..48067fc 100644 --- a/lib/rack/attack/store_proxy/dalli_proxy.rb +++ b/lib/rack/attack/store_proxy/dalli_proxy.rb @@ -12,7 +12,7 @@ module Rack if defined?(::ConnectionPool) && store.is_a?(::ConnectionPool) store.with { |conn| conn.is_a?(::Dalli::Client) } else - store.is_a?(::Dalli::Client) + store.is_a?(::Dalli::Client) || store.is_a?(::ActiveSupport::Cache::MemCacheStore) end end diff --git a/spec/integration/rack_attack_cache_spec.rb b/spec/integration/rack_attack_cache_spec.rb index 06aa51a..6eb27ef 100644 --- a/spec/integration/rack_attack_cache_spec.rb +++ b/spec/integration/rack_attack_cache_spec.rb @@ -17,12 +17,14 @@ describe Rack::Attack::Cache do end require 'active_support/cache/dalli_store' + require 'active_support/cache/mem_cache_store' require 'active_support/cache/redis_store' require 'connection_pool' cache_stores = [ ActiveSupport::Cache::MemoryStore.new, ActiveSupport::Cache::DalliStore.new("127.0.0.1"), ActiveSupport::Cache::RedisStore.new("127.0.0.1"), + ActiveSupport::Cache::MemCacheStore.new("127.0.0.1"), Dalli::Client.new, ConnectionPool.new { Dalli::Client.new }, Redis::Store.new @@ -54,6 +56,7 @@ describe Rack::Attack::Cache do @cache.send(:do_count, @key, @expires_in).must_equal 2 end end + describe "do_count after expires_in" do it "must be 1" do @cache.send(:do_count, @key, @expires_in)