mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Caching: MemCache and Redis stores use local cache for multi-reads
Fixes #31909. Closes #31911.
This commit is contained in:
parent
5ecbeda0e2
commit
1077ae96b3
3 changed files with 32 additions and 0 deletions
|
@ -1,5 +1,10 @@
|
|||
## Rails 6.0.0.alpha (Unreleased) ##
|
||||
|
||||
* Caching: MemCache and Redis `read_multi` and `fetch_multi` speedup.
|
||||
Read from the local in-memory cache before consulting the backend.
|
||||
|
||||
*Gabriel Sobrinho*
|
||||
|
||||
* Return all mappings for a timezone identifier in `country_zones`
|
||||
|
||||
Some timezones like `Europe/London` have multiple mappings in
|
||||
|
|
|
@ -54,6 +54,10 @@ module ActiveSupport
|
|||
@data[key]
|
||||
end
|
||||
|
||||
def read_multi_entries(keys, options)
|
||||
Hash[keys.map { |name| [name, read_entry(name, options)] }.keep_if { |_name, value| value }]
|
||||
end
|
||||
|
||||
def write_entry(key, value, options)
|
||||
@data[key] = value
|
||||
true
|
||||
|
@ -116,6 +120,19 @@ module ActiveSupport
|
|||
end
|
||||
end
|
||||
|
||||
def read_multi_entries(keys, options)
|
||||
return super unless local_cache
|
||||
|
||||
local_entries = local_cache.read_multi_entries(keys, options)
|
||||
missed_keys = keys - local_entries.keys
|
||||
|
||||
if missed_keys.any?
|
||||
local_entries.merge!(super(missed_keys, options))
|
||||
else
|
||||
local_entries
|
||||
end
|
||||
end
|
||||
|
||||
def write_entry(key, entry, options)
|
||||
if options[:unless_exist]
|
||||
local_cache.delete_entry(key, options) if local_cache
|
||||
|
|
|
@ -119,6 +119,16 @@ module LocalCacheBehavior
|
|||
end
|
||||
end
|
||||
|
||||
def test_local_cache_of_fetch_multi
|
||||
@cache.with_local_cache do
|
||||
@cache.fetch_multi("foo", "bar") { |_key| true }
|
||||
@peek.delete("foo")
|
||||
@peek.delete("bar")
|
||||
assert_equal true, @cache.read("foo")
|
||||
assert_equal true, @cache.read("bar")
|
||||
end
|
||||
end
|
||||
|
||||
def test_middleware
|
||||
app = lambda { |env|
|
||||
result = @cache.write("foo", "bar")
|
||||
|
|
Loading…
Reference in a new issue