Skip to content

Commit

Permalink
Fix reset! when using namespaced cache store
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed Nov 4, 2024
1 parent 427fdfa commit cf62482
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rack/attack/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def delete(unprefixed_key)

def reset!
if store.respond_to?(:delete_matched)
store.delete_matched("#{prefix}*")
store.delete_matched(/#{prefix}*/)
else
raise(
Rack::Attack::IncompatibleStoreError,
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/attack/store_proxy/redis_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def delete_matched(matcher, _options = nil)
rescuing do
# Fetch keys in batches using SCAN to avoid blocking the Redis server.
loop do
cursor, keys = scan(cursor, match: matcher, count: 1000)
cursor, keys = scan(cursor, match: matcher.source, count: 1000)
del(*keys) unless keys.empty?
break if cursor == "0"
end
Expand Down
13 changes: 13 additions & 0 deletions spec/rack_attack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,18 @@
_(redis.get("#{Rack::Attack.cache.prefix}::key")).must_be_nil
end
end

if defined?(ActiveSupport::Cache::MemoryStore)
it 'should delete rack attack keys' do
memory_store = ActiveSupport::Cache::MemoryStore.new(namespace: 'ns')
memory_store.write('key', 'value')
memory_store.write("#{Rack::Attack.cache.prefix}::key", 'value')
Rack::Attack.cache.store = memory_store
Rack::Attack.reset!

_(memory_store.read('key')).must_equal 'value'
_(memory_store.read("#{Rack::Attack.cache.prefix}::key")).must_be_nil
end
end
end
end

0 comments on commit cf62482

Please sign in to comment.