rack-attack/lib/rack/attack/cache.rb
2012-08-15 18:16:12 -04:00

25 lines
688 B
Ruby

module Rack
module Attack
class Cache
attr_accessor :store, :prefix
def initialize
@store = ::Rails.cache if defined?(::Rails.cache)
@prefix = 'rack::attack'
end
def count(unprefixed_key, period)
epoch_time = Time.now.to_i
expires_in = period - (epoch_time % period)
key = "#{prefix}:#{(epoch_time/period).to_i}:#{unprefixed_key}"
result = store.increment(key, 1, :expires_in => expires_in)
# NB: Some stores return nil when incrementing uninitialized values
if result.nil?
store.write(key, 1, :expires_in => expires_in)
end
result || 1
end
end
end
end