mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Merge pull request #53 from hakanensari/patch-1
Implement Proxy for Dalli
This commit is contained in:
commit
d3026acef5
2 changed files with 63 additions and 0 deletions
|
|
@ -14,6 +14,8 @@ module Rack
|
|||
|
||||
if defined?(::Redis::Store) && store.is_a?(::Redis::Store)
|
||||
RedisStoreProxy.new(store)
|
||||
elsif defined?(::Dalli) && store.is_a?(::Dalli::Client)
|
||||
DalliProxy.new(store)
|
||||
else
|
||||
store
|
||||
end
|
||||
|
|
@ -52,6 +54,34 @@ module Rack
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class DalliProxy < SimpleDelegator
|
||||
def initialize(client)
|
||||
super(client)
|
||||
end
|
||||
|
||||
def read(key)
|
||||
with do |client|
|
||||
client.get(key)
|
||||
end
|
||||
rescue Dalli::DalliError
|
||||
end
|
||||
|
||||
def write(key, value, options={})
|
||||
with do |client|
|
||||
client.set(key, value, options.fetch(:expires_in, 0), raw: true)
|
||||
end
|
||||
rescue Dalli::DalliError
|
||||
end
|
||||
|
||||
def increment(key, amount, options={})
|
||||
with do |client|
|
||||
client.incr(key, amount, options.fetch(:expires_in, 0), amount)
|
||||
end
|
||||
rescue Dalli::DalliError
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ describe Rack::Attack::Cache do
|
|||
ActiveSupport::Cache::MemoryStore.new,
|
||||
ActiveSupport::Cache::DalliStore.new("localhost"),
|
||||
ActiveSupport::Cache::RedisStore.new("localhost"),
|
||||
Dalli::Client.new,
|
||||
Redis::Store.new
|
||||
]
|
||||
|
||||
|
|
@ -108,4 +109,36 @@ describe Rack::Attack::Cache do
|
|||
end
|
||||
end
|
||||
|
||||
describe "should not error if memcached is not running" do
|
||||
before {
|
||||
Dalli.logger.level = Logger::FATAL
|
||||
@cache = Rack::Attack::Cache.new
|
||||
@key = "rack::attack:cache-test-key"
|
||||
@expires_in = 1
|
||||
@cache.store = Dalli::Client.new('127.0.0.1:22122')
|
||||
}
|
||||
|
||||
after {
|
||||
Dalli.logger.level = Logger::INFO
|
||||
}
|
||||
|
||||
describe "write" do
|
||||
it "should not raise exception" do
|
||||
@cache.write("cache-test-key", "foobar", 1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "read" do
|
||||
it "should not raise exception" do
|
||||
@cache.read("cache-test-key")
|
||||
end
|
||||
end
|
||||
|
||||
describe "do_count" do
|
||||
it "should not raise exception" do
|
||||
@cache.send(:do_count, @key, @expires_in)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue