diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 49485e19660..183b596f967 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -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 diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 87cda4cf81f..ef1cf051ff7 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -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 diff --git a/activesupport/test/cache/behaviors/local_cache_behavior.rb b/activesupport/test/cache/behaviors/local_cache_behavior.rb index e8cc4b03e4d..f45230afb89 100644 --- a/activesupport/test/cache/behaviors/local_cache_behavior.rb +++ b/activesupport/test/cache/behaviors/local_cache_behavior.rb @@ -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")