From d9fedfae4f7f6409f33857763391f4e18a6d7467 Mon Sep 17 00:00:00 2001 From: Jonathan del Strother Date: Mon, 20 Nov 2023 16:59:08 +0000 Subject: [PATCH] Avoid RedisCacheStore#increment on Rails 6+ (#597) Rails has handled increment+expires_in since 6.0 (https://github.com/rails/rails/commit/9d5b02ec5062a23665ec596ef7d3efe4f5abcc27), and more recent versions add pipeline optimizations on top of that. --- .../store_proxy/redis_cache_store_proxy.rb | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb b/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb index 00670f06..4fe42002 100644 --- a/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb +++ b/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb @@ -10,17 +10,19 @@ def self.handle?(store) store.class.name == "ActiveSupport::Cache::RedisCacheStore" end - def increment(name, amount = 1, **options) - # RedisCacheStore#increment ignores options[:expires_in]. - # - # So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize - # the counter. After that we continue using the original RedisCacheStore#increment. - if options[:expires_in] && !read(name) - write(name, amount, options) + if defined?(::ActiveSupport) && ::ActiveSupport::VERSION::MAJOR < 6 + def increment(name, amount = 1, **options) + # RedisCacheStore#increment ignores options[:expires_in] in versions prior to 6. + # + # So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize + # the counter. After that we continue using the original RedisCacheStore#increment. + if options[:expires_in] && !read(name) + write(name, amount, options) - amount - else - super + amount + else + super + end end end