Class: Rack::Attack::Cache
- Inherits:
-
Object
- Object
- Rack::Attack::Cache
- Defined in:
- lib/rack/attack/cache.rb
Instance Attribute Summary collapse
-
#last_epoch_time ⇒ Object
readonly
Returns the value of attribute last_epoch_time.
-
#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
- #delete(unprefixed_key) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #read(unprefixed_key) ⇒ Object
- #reset! ⇒ Object
- #reset_count(unprefixed_key, period) ⇒ Object
- #write(unprefixed_key, value, expires_in) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
9 10 11 12 |
# File 'lib/rack/attack/cache.rb', line 9 def initialize self.store = ::Rails.cache if defined?(::Rails.cache) @prefix = 'rack::attack' end |
Instance Attribute Details
#last_epoch_time ⇒ Object (readonly)
Returns the value of attribute last_epoch_time.
7 8 9 |
# File 'lib/rack/attack/cache.rb', line 7 def last_epoch_time @last_epoch_time end |
#prefix ⇒ Object
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/rack/attack/cache.rb', line 6 def prefix @prefix end |
#store ⇒ Object
Returns the value of attribute store.
14 15 16 |
# File 'lib/rack/attack/cache.rb', line 14 def store @store end |
Instance Method Details
#count(unprefixed_key, period) ⇒ Object
25 26 27 28 |
# File 'lib/rack/attack/cache.rb', line 25 def count(unprefixed_key, period) key, expires_in = key_and_expiry(unprefixed_key, period) do_count(key, expires_in) end |
#delete(unprefixed_key) ⇒ Object
46 47 48 |
# File 'lib/rack/attack/cache.rb', line 46 def delete(unprefixed_key) store.delete("#{prefix}:#{unprefixed_key}") end |
#read(unprefixed_key) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/rack/attack/cache.rb', line 30 def read(unprefixed_key) enforce_store_presence! enforce_store_method_presence!(:read) store.read("#{prefix}:#{unprefixed_key}") end |
#reset! ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rack/attack/cache.rb', line 50 def reset! if store.respond_to?(:delete_matched) store.delete_matched("#{prefix}*") else raise( Rack::Attack::IncompatibleStoreError, "Configured store #{store.class.name} doesn't respond to #delete_matched method" ) end end |
#reset_count(unprefixed_key, period) ⇒ Object
41 42 43 44 |
# File 'lib/rack/attack/cache.rb', line 41 def reset_count(unprefixed_key, period) key, _ = key_and_expiry(unprefixed_key, period) store.delete(key) end |
#write(unprefixed_key, value, expires_in) ⇒ Object
37 38 39 |
# File 'lib/rack/attack/cache.rb', line 37 def write(unprefixed_key, value, expires_in) store.write("#{prefix}:#{unprefixed_key}", value, expires_in: expires_in) end |