Merge pull request #40490 from kirs/cache-instrument-store-local

Instrument cache entries from local cache
This commit is contained in:
Rafael França 2020-11-05 16:36:50 -05:00 committed by GitHub
commit 53e97f0fa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View File

@ -324,7 +324,7 @@ module ActiveSupport
entry = nil
instrument(:read, name, options) do |payload|
cached_entry = read_entry(key, **options) unless options[:force]
cached_entry = read_entry(key, **options, event: payload) unless options[:force]
entry = handle_expired_entry(cached_entry, key, options)
entry = nil if entry && entry.mismatched?(normalize_version(name, options))
payload[:super_operation] = :fetch if payload
@ -358,7 +358,7 @@ module ActiveSupport
version = normalize_version(name, options)
instrument(:read, name, options) do |payload|
entry = read_entry(key, **options)
entry = read_entry(key, **options, event: payload)
if entry
if entry.expired?
@ -390,7 +390,7 @@ module ActiveSupport
options = merged_options(options)
instrument :read_multi, names, options do |payload|
read_multi_entries(names, **options).tap do |results|
read_multi_entries(names, **options, event: payload).tap do |results|
payload[:hits] = results.keys
end
end
@ -500,8 +500,8 @@ module ActiveSupport
def exist?(name, options = nil)
options = merged_options(options)
instrument(:exist?, name) do
entry = read_entry(normalize_key(name, options), **options)
instrument(:exist?, name) do |payload|
entry = read_entry(normalize_key(name, options), **options, event: payload)
(entry && !entry.expired? && !entry.mismatched?(normalize_version(name, options))) || false
end
end

View File

@ -130,7 +130,13 @@ module ActiveSupport
private
def read_entry(key, **options)
if cache = local_cache
cache.fetch_entry(key) { super }
hit = true
value = cache.fetch_entry(key) do
hit = false
super
end
options[:event][:store] = cache.class.name if hit
value
else
super
end

View File

@ -1,6 +1,23 @@
# frozen_string_literal: true
module LocalCacheBehavior
def test_instrumentation_with_local_cache
events = with_instrumentation "write" do
@cache.write("foo", "bar")
end
assert_equal @cache.class.name, events[0].payload[:store]
events = with_instrumentation "read" do
@cache.with_local_cache do
@cache.read("foo")
@cache.read("foo")
end
end
expected = [@cache.class.name, "ActiveSupport::Cache::Strategy::LocalCache::LocalStore"]
assert_equal expected, events.map { |p| p.payload[:store] }
end
def test_local_writes_are_persistent_on_the_remote_cache
retval = @cache.with_local_cache do
@cache.write("foo", "bar")