Skip to content

Commit

Permalink
Merge pull request #40490 from kirs/cache-instrument-store-local
Browse files Browse the repository at this point in the history
Instrument cache entries from local cache
  • Loading branch information
rafaelfranca committed Nov 5, 2020
2 parents a17c5ff + c882056 commit 53e97f0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
10 changes: 5 additions & 5 deletions activesupport/lib/active_support/cache.rb
Expand Up @@ -324,7 +324,7 @@ def fetch(name, options = nil, &block)

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
Expand Down Expand Up @@ -358,7 +358,7 @@ def read(name, options = nil)
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?
Expand Down Expand Up @@ -390,7 +390,7 @@ def read_multi(*names)
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
Expand Down Expand Up @@ -500,8 +500,8 @@ def delete_multi(names, options = nil)
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
Expand Down
Expand Up @@ -130,7 +130,13 @@ def decrement(name, amount = 1, **options) # :nodoc:
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
Expand Down
17 changes: 17 additions & 0 deletions 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")
Expand Down

0 comments on commit 53e97f0

Please sign in to comment.