From e27e9e7b485f3f2023de323e6f5dc0b3a9ea103d Mon Sep 17 00:00:00 2001 From: Jonathan del Strother Date: Mon, 21 Nov 2022 13:20:13 +0000 Subject: [PATCH] Avoid RedisCacheStore#increment on Rails 6+ 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..351f39a7 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