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"
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