Avoid RedisCacheStore#increment on Rails 6+ (#597)

Rails has handled increment+expires_in since
6.0 (9d5b02ec50),
and more recent versions add pipeline optimizations on top of that.
This commit is contained in:
Jonathan del Strother 2023-11-20 16:59:08 +00:00 committed by GitHub
parent 27ada96682
commit d9fedfae4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,17 +10,19 @@ module Rack
store.class.name == "ActiveSupport::Cache::RedisCacheStore" store.class.name == "ActiveSupport::Cache::RedisCacheStore"
end end
def increment(name, amount = 1, **options) if defined?(::ActiveSupport) && ::ActiveSupport::VERSION::MAJOR < 6
# RedisCacheStore#increment ignores options[:expires_in]. 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. # So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
if options[:expires_in] && !read(name) # the counter. After that we continue using the original RedisCacheStore#increment.
write(name, amount, options) if options[:expires_in] && !read(name)
write(name, amount, options)
amount amount
else else
super super
end
end end
end end