class Rack::Attack::StoreProxy::RedisProxy

Public Class Methods

handle?(store) click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 17
def self.handle?(store)
  defined?(::Redis) && store.class == ::Redis
end
new(*args) click to toggle source
Calls superclass method
# 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

Public Instance Methods

delete(key, _options = {}) click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 42
def delete(key, _options = {})
  rescuing { del(key) }
end
delete_matched(matcher, _options = nil) click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 46
def delete_matched(matcher, _options = nil)
  cursor = "0"

  rescuing do
    # Fetch keys in batches using SCAN to avoid blocking the Redis server.
    loop do
      cursor, keys = scan(cursor, match: matcher, count: 1000)
      del(*keys) unless keys.empty?
      break if cursor == "0"
    end
  end
end
increment(key, amount, options = {}) click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 33
def increment(key, amount, options = {})
  rescuing do
    pipelined do |redis|
      redis.incrby(key, amount)
      redis.expire(key, options[:expires_in]) if options[:expires_in]
    end.first
  end
end
read(key) click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 21
def read(key)
  rescuing { get(key) }
end
write(key, value, options = {}) click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 25
def write(key, value, options = {})
  if (expires_in = options[:expires_in])
    rescuing { setex(key, expires_in, value) }
  else
    rescuing { set(key, value) }
  end
end

Private Instance Methods

rescuing() { || ... } click to toggle source
# File lib/rack/attack/store_proxy/redis_proxy.rb, line 61
def rescuing
  yield
rescue Redis::BaseConnectionError
  nil
end