Class: RateAttack::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/rate_attack/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



7
8
9
10
# File 'lib/rate_attack/cache.rb', line 7

def initialize
  self.store = ::Rails.cache if defined?(::Rails.cache)
  @prefix = 'rate_attack'
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



5
6
7
# File 'lib/rate_attack/cache.rb', line 5

def prefix
  @prefix
end

#storeObject

Returns the value of attribute store.



12
13
14
# File 'lib/rate_attack/cache.rb', line 12

def store
  @store
end

Instance Method Details

#count(unprefixed_key, period) ⇒ Object



18
19
20
21
# File 'lib/rate_attack/cache.rb', line 18

def count(unprefixed_key, period)
  key, expires_in = key_and_expiry(unprefixed_key, period)
  do_count(key, expires_in)
end

#read(unprefixed_key) ⇒ Object



23
24
25
# File 'lib/rate_attack/cache.rb', line 23

def read(unprefixed_key)
  store.read("#{prefix}:#{unprefixed_key}")
end

#write(unprefixed_key, value, expires_in) ⇒ Object



27
28
29
# File 'lib/rate_attack/cache.rb', line 27

def write(unprefixed_key, value, expires_in)
  store.write("#{prefix}:#{unprefixed_key}", value, expires_in: expires_in)
end