mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-31 10:25:54 +00:00
While a cache-store proxy exists for the redis-store gem, no such proxy existed for using the redis gem itself. This fills that gap by adding such a proxy. Resolves kickstarter/rack-attack#190
44 lines
887 B
Ruby
44 lines
887 B
Ruby
require 'delegate'
|
|
|
|
module Rack
|
|
class Attack
|
|
module StoreProxy
|
|
class RedisProxy < SimpleDelegator
|
|
def self.handle?(store)
|
|
defined?(::Redis) && store.is_a?(::Redis)
|
|
end
|
|
|
|
def initialize(store)
|
|
super(store)
|
|
end
|
|
|
|
def read(key)
|
|
get(key)
|
|
end
|
|
|
|
def write(key, value, options={})
|
|
if (expires_in = options[:expires_in])
|
|
setex(key, expires_in, value)
|
|
else
|
|
set(key, value)
|
|
end
|
|
end
|
|
|
|
def increment(key, amount, options={})
|
|
count = nil
|
|
|
|
pipelined do
|
|
count = incrby(key, amount)
|
|
expire(key, options[:expires_in]) if options[:expires_in]
|
|
end
|
|
|
|
count.value if count
|
|
end
|
|
|
|
def delete(key, options={})
|
|
del(key)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|