Class: Rack::Attack::Cache
- Inherits:
-
Object
- Object
- Rack::Attack::Cache
- Defined in:
- lib/rack/attack/cache.rb
Instance Attribute Summary collapse
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #count(unprefixed_key, period) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #read(unprefixed_key) ⇒ Object
- #write(unprefixed_key, value, expires_in) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 |
# File 'lib/rack/attack/cache.rb', line 7 def initialize self.store = ::Rails.cache if defined?(::Rails.cache) @prefix = 'rack::attack' end |
Instance Attribute Details
#prefix ⇒ Object
Returns the value of attribute prefix.
5 6 7 |
# File 'lib/rack/attack/cache.rb', line 5 def prefix @prefix end |
#store ⇒ Object
Returns the value of attribute store.
12 13 14 |
# File 'lib/rack/attack/cache.rb', line 12 def store @store end |
Instance Method Details
#count(unprefixed_key, period) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/rack/attack/cache.rb', line 17 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}" do_count(key, expires_in) end |
#read(unprefixed_key) ⇒ Object
24 25 26 |
# File 'lib/rack/attack/cache.rb', line 24 def read(unprefixed_key) store.read("#{prefix}:#{unprefixed_key}") end |
#write(unprefixed_key, value, expires_in) ⇒ Object
28 29 30 |
# File 'lib/rack/attack/cache.rb', line 28 def write(unprefixed_key, value, expires_in) store.write("#{prefix}:#{unprefixed_key}", value, :expires_in => expires_in) end |