11# frozen_string_literal: true
22
3- require 'rack/attack/base_proxy '
3+ require 'rack/attack/store_adapter '
44
55module Rack
66 class Attack
7- module StoreProxy
8- class RedisProxy < BaseProxy
9- def initialize ( * args )
7+ module StoreAdapters
8+ class RedisAdapter < StoreAdapter
9+ def initialize ( store )
1010 if Gem ::Version . new ( Redis ::VERSION ) < Gem ::Version . new ( "3" )
1111 warn 'RackAttack requires Redis gem >= 3.0.0.'
1212 end
1313
14- super ( * args )
14+ super
1515 end
1616
1717 def self . handle? ( store )
1818 defined? ( ::Redis ) && store . class == ::Redis
1919 end
2020
2121 def read ( key )
22- rescuing { get ( key ) }
22+ rescuing { store . get ( key ) }
2323 end
2424
2525 def write ( key , value , options = { } )
2626 if ( expires_in = options [ :expires_in ] )
27- rescuing { setex ( key , expires_in , value ) }
27+ rescuing { store . setex ( key , expires_in , value ) }
2828 else
29- rescuing { set ( key , value ) }
29+ rescuing { store . set ( key , value ) }
3030 end
3131 end
3232
3333 def increment ( key , amount , options = { } )
3434 rescuing do
35- pipelined do
36- incrby ( key , amount )
37- expire ( key , options [ :expires_in ] ) if options [ :expires_in ]
35+ store . pipelined do
36+ store . incrby ( key , amount )
37+ store . expire ( key , options [ :expires_in ] ) if options [ :expires_in ]
3838 end . first
3939 end
4040 end
4141
4242 def delete ( key , _options = { } )
43- rescuing { del ( key ) }
43+ rescuing { store . del ( key ) }
4444 end
4545
4646 def delete_matched ( matcher , _options = nil )
@@ -49,8 +49,8 @@ def delete_matched(matcher, _options = nil)
4949 rescuing do
5050 # Fetch keys in batches using SCAN to avoid blocking the Redis server.
5151 loop do
52- cursor , keys = scan ( cursor , match : matcher , count : 1000 )
53- del ( *keys ) unless keys . empty?
52+ cursor , keys = store . scan ( cursor , match : matcher , count : 1000 )
53+ store . del ( *keys ) unless keys . empty?
5454 break if cursor == "0"
5555 end
5656 end
0 commit comments