Class: Rack::Attack::StoreProxy::RedisProxy

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/rack/attack/store_proxy/redis_proxy.rb

Direct Known Subclasses

RedisStoreProxy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RedisProxy

Returns a new instance of RedisProxy.



9
10
11
12
13
14
15
# File 'lib/rack/attack/store_proxy/redis_proxy.rb', line 9

def initialize(*args)
  if Gem::Version.new(Redis::VERSION) < Gem::Version.new("3")
    warn 'RackAttack requires Redis gem >= 3.0.0.'
  end

  super(*args)
end

Class Method Details

.handle?(store) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rack/attack/store_proxy/redis_proxy.rb', line 17

def self.handle?(store)
  defined?(::Redis) && store.is_a?(::Redis)
end

Instance Method Details

#delete(key, _options = {}) ⇒ Object



47
48
49
50
# File 'lib/rack/attack/store_proxy/redis_proxy.rb', line 47

def delete(key, _options = {})
  del(key)
rescue Redis::BaseError
end

#increment(key, amount, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rack/attack/store_proxy/redis_proxy.rb', line 35

def increment(key, amount, options = {})
  count = nil

  pipelined do
    count = incrby(key, amount)
    expire(key, options[:expires_in]) if options[:expires_in]
  end

  count.value if count
rescue Redis::BaseError
end

#read(key) ⇒ Object



21
22
23
24
# File 'lib/rack/attack/store_proxy/redis_proxy.rb', line 21

def read(key)
  get(key)
rescue Redis::BaseError
end

#write(key, value, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rack/attack/store_proxy/redis_proxy.rb', line 26

def write(key, value, options = {})
  if (expires_in = options[:expires_in])
    setex(key, expires_in, value)
  else
    set(key, value)
  end
rescue Redis::BaseError
end