Skip to content
Permalink
Browse files

Caching: MemCache and Redis stores use local cache for multi-reads

Fixes #31909.
Closes #31911.
  • Loading branch information...
sobrinho authored and jeremy committed Feb 6, 2018
1 parent df4ed0c commit a2b97e4ffef971607a1be8fc7909f099b6840f36
@@ -1,3 +1,8 @@
* 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 @@ def read_entry(key, options)
@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 @@ def read_entry(key, options)
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 @@ def test_local_cache_of_decrement
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")

0 comments on commit a2b97e4

Please sign in to comment.
You can’t perform that action at this time.