rack-attack/lib/rack/attack/cache.rb
Aaron Suggs 853c9ceef3 Set :expires_in on throttle cache counters
Should reduce memcached evictions
2012-08-09 11:05:30 -04:00

25 lines
681 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}:#{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