Skip to content

Commit cf62482

Browse files
committed
Fix reset! when using namespaced cache store
1 parent 427fdfa commit cf62482

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

lib/rack/attack/cache.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def delete(unprefixed_key)
5555

5656
def reset!
5757
if store.respond_to?(:delete_matched)
58-
store.delete_matched("#{prefix}*")
58+
store.delete_matched(/#{prefix}*/)
5959
else
6060
raise(
6161
Rack::Attack::IncompatibleStoreError,

lib/rack/attack/store_proxy/redis_proxy.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ 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)
52+
cursor, keys = scan(cursor, match: matcher.source, count: 1000)
5353
del(*keys) unless keys.empty?
5454
break if cursor == "0"
5555
end

spec/rack_attack_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,18 @@
124124
_(redis.get("#{Rack::Attack.cache.prefix}::key")).must_be_nil
125125
end
126126
end
127+
128+
if defined?(ActiveSupport::Cache::MemoryStore)
129+
it 'should delete rack attack keys' do
130+
memory_store = ActiveSupport::Cache::MemoryStore.new(namespace: 'ns')
131+
memory_store.write('key', 'value')
132+
memory_store.write("#{Rack::Attack.cache.prefix}::key", 'value')
133+
Rack::Attack.cache.store = memory_store
134+
Rack::Attack.reset!
135+
136+
_(memory_store.read('key')).must_equal 'value'
137+
_(memory_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil
138+
end
139+
end
127140
end
128141
end

0 commit comments

Comments
 (0)