mirror of https://github.com/rails/rails
Merge pull request #40490 from kirs/cache-instrument-store-local
Instrument cache entries from local cache
This commit is contained in:
commit
53e97f0fa0
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue