mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Don't cache locally if unless_exist was passed
Some cache backends support the `unless_exist` option, which tells them not to overwrite an existing entry. The local cache currently always stores the new value, even though the backend may have rejected it. Since we can't tell which value will end up in the backend cache, we should delete the key from the local cache, so that the next read for that key will go to the backend and pick up the correct value.
This commit is contained in:
parent
d48008f164
commit
db9ae5f1e1
2 changed files with 14 additions and 1 deletions
|
@ -115,7 +115,12 @@ module ActiveSupport
|
|||
end
|
||||
|
||||
def write_entry(key, entry, options)
|
||||
local_cache.write_entry(key, entry, options) if local_cache
|
||||
if options[:unless_exist]
|
||||
local_cache.delete_entry(key, options) if local_cache
|
||||
else
|
||||
local_cache.write_entry(key, entry, options) if local_cache
|
||||
end
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
|
|
|
@ -708,6 +708,14 @@ module LocalCacheBehavior
|
|||
end
|
||||
end
|
||||
|
||||
def test_local_cache_of_write_with_unless_exist
|
||||
@cache.with_local_cache do
|
||||
@cache.write("foo", "bar")
|
||||
@cache.write("foo", "baz", unless_exist: true)
|
||||
assert_equal @peek.read("foo"), @cache.read("foo")
|
||||
end
|
||||
end
|
||||
|
||||
def test_local_cache_of_delete
|
||||
@cache.with_local_cache do
|
||||
@cache.write("foo", "bar")
|
||||
|
|
Loading…
Reference in a new issue