Fix MemoryStore#write with unless_exist and namespace

Updates `MemoryStore#write_entry` to pass a `nil` `namespace` to
`exist?`, which expects a _name_ rather than a an already "normalized"
_key_. This fixes a bug where `unless_exist` would overwrite any
existing entry if a `namespace` was used.
This commit is contained in:
S. Brent Faulkner 2023-11-28 09:44:03 -05:00
parent ed2bc92b82
commit 701377c6af
No known key found for this signature in database
GPG Key ID: 9DD8B9A2FB33D94F
2 changed files with 23 additions and 14 deletions

View File

@ -210,7 +210,7 @@ module ActiveSupport
def write_entry(key, entry, **options)
payload = serialize_entry(entry, **options)
synchronize do
return false if options[:unless_exist] && exist?(key)
return false if options[:unless_exist] && exist?(key, namespace: nil)
old_payload = @data[key]
if old_payload

View File

@ -61,6 +61,28 @@ class MemoryStoreTest < ActiveSupport::TestCase
assert_same value, @cache.read("key")
end
def test_write_with_unless_exist
assert_equal true, @cache.write(1, "aaaaaaaaaa")
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)
@cache.write(1, nil)
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)
end
def test_namespaced_write_with_unless_exist
namespaced_cache = lookup_store(expires_in: 60, namespace: "foo")
assert_equal true, namespaced_cache.write(1, "aaaaaaaaaa")
assert_equal false, namespaced_cache.write(1, "aaaaaaaaaa", unless_exist: true)
namespaced_cache.write(1, nil)
assert_equal false, namespaced_cache.write(1, "aaaaaaaaaa", unless_exist: true)
end
def test_write_expired_value_with_unless_exist
assert_equal true, @cache.write(1, "aaaa", expires_in: 1.second)
travel 2.seconds
assert_equal true, @cache.write(1, "bbbb", expires_in: 1.second, unless_exist: true)
end
private
def compression_always_disabled_by_default?
true
@ -187,17 +209,4 @@ class MemoryStorePruningTest < ActiveSupport::TestCase
assert_not_equal item.object_id, read_item.object_id
assert_not_equal read_item.object_id, @cache.read(key).object_id
end
def test_write_with_unless_exist
assert_equal true, @cache.write(1, "aaaaaaaaaa")
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)
@cache.write(1, nil)
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)
end
def test_write_expired_value_with_unless_exist
assert_equal true, @cache.write(1, "aaaa", expires_in: 1.second)
travel 2.seconds
assert_equal true, @cache.write(1, "bbbb", expires_in: 1.second, unless_exist: true)
end
end