mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
23 lines
577 B
Ruby
23 lines
577 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, expires_in)
|
|
key = "#{prefix}:#{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
|