Using write with unless_exist + expires_in should unlock after the given expires_in and not 5 minutes later (#40260)

Co-authored-by: Jye Lee <jyelee124@gmail.com>
Co-authored-by: Eugene Kenny <elkenny@gmail.com>
This commit is contained in:
Michael Grosser 2020-09-20 13:07:35 -07:00 committed by GitHub
parent 5bf8b3e07d
commit 950bf54cda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

@ -1,3 +1,7 @@
* Fix bug to make memcached write_entry expire correctly with unless_exist
*Jye Lee*
* Add `ActiveSupport::Duration` conversion methods
`in_seconds`, `in_minutes`, `in_hours`, `in_days`, `in_weeks`, `in_months`, and `in_years` return the respective duration covered.

@ -144,7 +144,7 @@ def write_entry(key, entry, **options)
method = options[:unless_exist] ? :add : :set
value = options[:raw] ? entry.value.to_s : entry
expires_in = options[:expires_in].to_i
if expires_in > 0 && !options[:raw]
if options[:race_condition_ttl] && expires_in > 0 && !options[:raw]
# Set the memcache expire a few minutes in the future to support race condition ttls on read
expires_in += 5.minutes
end

@ -161,6 +161,13 @@ def test_no_multiple_compress
end
end
def test_unless_exist_expires_when_configured
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
assert_called_with cache.instance_variable_get(:@data), :add, [ "foo", ActiveSupport::Cache::Entry, 1, Hash ] do
cache.write("foo", "bar", expires_in: 1, unless_exist: true)
end
end
private
def random_string(length)
(0...length).map { (65 + rand(26)).chr }.join