Skip to content

Commit

Permalink
Avoid RedisCacheStore#increment on Rails 6+ (#597)
Browse files Browse the repository at this point in the history
Rails has handled increment+expires_in since
6.0 (rails/rails@9d5b02e),
and more recent versions add pipeline optimizations on top of that.
  • Loading branch information
jdelStrother committed Nov 20, 2023
1 parent 27ada96 commit d9fedfa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/rack/attack/store_proxy/redis_cache_store_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d9fedfa

Please sign in to comment.