Skip to content

Commit 74517c3

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

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-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

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ def delete(key, _options = {})
4545

4646
def delete_matched(matcher, _options = nil)
4747
cursor = "0"
48+
source = matcher.source
4849

4950
rescuing do
5051
# Fetch keys in batches using SCAN to avoid blocking the Redis server.
5152
loop do
52-
cursor, keys = scan(cursor, match: matcher, count: 1000)
53+
cursor, keys = scan(cursor, match: source, count: 1000)
5354
del(*keys) unless keys.empty?
5455
break if cursor == "0"
5556
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)