Skip to content

Commit a8800c0

Browse files
authored
Merge pull request rails#52723 from frederikspang/feature/instrument-memorystore
Instrument ActiveSupport::Cache::MemoryStore#increment and #decrement
2 parents 325d273 + b602ffd commit a8800c0

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,11 @@ def read_multi(*names)
538538

539539
options = names.extract_options!
540540
options = merged_options(options)
541+
keys = names.map { |name| normalize_key(name, options) }
541542

542-
instrument_multi :read_multi, names, options do |payload|
543+
instrument_multi :read_multi, keys, options do |payload|
543544
read_multi_entries(names, **options, event: payload).tap do |results|
544-
payload[:hits] = results.keys
545+
payload[:hits] = results.keys.map { |name| normalize_key(name, options) }
545546
end
546547
end
547548
end
@@ -551,8 +552,9 @@ def write_multi(hash, options = nil)
551552
return hash if hash.empty?
552553

553554
options = merged_options(options)
555+
normalized_hash = hash.transform_keys { |key| normalize_key(key, options) }
554556

555-
instrument_multi :write_multi, hash, options do |payload|
557+
instrument_multi :write_multi, normalized_hash, options do |payload|
556558
entries = hash.each_with_object({}) do |(name, value), memo|
557559
memo[normalize_key(name, options)] = Entry.new(value, **options.merge(version: normalize_version(name, options)))
558560
end
@@ -596,9 +598,9 @@ def fetch_multi(*names)
596598

597599
options = names.extract_options!
598600
options = merged_options(options)
599-
601+
keys = names.map { |name| normalize_key(name, options) }
600602
writes = {}
601-
ordered = instrument_multi :read_multi, names, options do |payload|
603+
ordered = instrument_multi :read_multi, keys, options do |payload|
602604
if options[:force]
603605
reads = {}
604606
else
@@ -610,7 +612,7 @@ def fetch_multi(*names)
610612
end
611613
writes.compact! if options[:skip_nil]
612614

613-
payload[:hits] = reads.keys
615+
payload[:hits] = reads.keys.map { |name| normalize_key(name, options) }
614616
payload[:super_operation] = :fetch_multi
615617

616618
ordered

activesupport/lib/active_support/cache/file_store.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ def cleanup(options = nil)
5858
# cache.increment("baz") # => 6
5959
#
6060
def increment(name, amount = 1, options = nil)
61-
modify_value(name, amount, options)
61+
options = merged_options(options)
62+
key = normalize_key(name, options)
63+
64+
instrument(:increment, key, amount: amount) do
65+
modify_value(name, amount, options)
66+
end
6267
end
6368

6469
# Decrement a cached integer value. Returns the updated value.
@@ -73,7 +78,12 @@ def increment(name, amount = 1, options = nil)
7378
# cache.decrement("baz") # => 4
7479
#
7580
def decrement(name, amount = 1, options = nil)
76-
modify_value(name, -amount, options)
81+
options = merged_options(options)
82+
key = normalize_key(name, options)
83+
84+
instrument(:decrement, key, amount: amount) do
85+
modify_value(name, -amount, options)
86+
end
7787
end
7888

7989
def delete_matched(matcher, options = nil)

activesupport/lib/active_support/cache/memory_store.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ def pruning?
147147
# cache.increment("baz") # => 6
148148
#
149149
def increment(name, amount = 1, options = nil)
150-
modify_value(name, amount, options)
150+
instrument(:increment, name, amount: amount) do
151+
modify_value(name, amount, options)
152+
end
151153
end
152154

153155
# Decrement a cached integer value. Returns the updated value.
@@ -162,7 +164,9 @@ def increment(name, amount = 1, options = nil)
162164
# cache.decrement("baz") # => 4
163165
#
164166
def decrement(name, amount = 1, options = nil)
165-
modify_value(name, -amount, options)
167+
instrument(:decrement, name, amount: amount) do
168+
modify_value(name, -amount, options)
169+
end
166170
end
167171

168172
# Deletes cache entries if the cache key matches a given pattern.

activesupport/lib/active_support/cache/redis_cache_store.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,12 @@ def read_multi(*names)
173173
return {} if names.empty?
174174

175175
options = names.extract_options!
176-
instrument_multi(:read_multi, names, options) do |payload|
176+
options = merged_options(options)
177+
keys = names.map { |name| normalize_key(name, options) }
178+
179+
instrument_multi(:read_multi, keys, options) do |payload|
177180
read_multi_entries(names, **options).tap do |results|
178-
payload[:hits] = results.keys
181+
payload[:hits] = results.keys.map { |name| normalize_key(name, options) }
179182
end
180183
end
181184
end

activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def test_write_multi_instrumentation
1414

1515
assert_equal %w[ cache_write_multi.active_support ], events.map(&:name)
1616
assert_nil events[0].payload[:super_operation]
17-
assert_equal({ key_1 => value_1, key_2 => value_2 }, events[0].payload[:key])
17+
assert_equal({ normalized_key(key_1) => value_1, normalized_key(key_2) => value_2 }, events[0].payload[:key])
1818
end
1919

2020
def test_instrumentation_with_fetch_multi_as_super_operation
@@ -29,8 +29,8 @@ def test_instrumentation_with_fetch_multi_as_super_operation
2929

3030
assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
3131
assert_equal :fetch_multi, events[0].payload[:super_operation]
32-
assert_equal [key_2, key_1], events[0].payload[:key]
33-
assert_equal [key_1], events[0].payload[:hits]
32+
assert_equal [normalized_key(key_2), normalized_key(key_1)], events[0].payload[:key]
33+
assert_equal [normalized_key(key_1)], events[0].payload[:hits]
3434
assert_equal @cache.class.name, events[0].payload[:store]
3535
end
3636

@@ -59,8 +59,35 @@ def test_read_multi_instrumentation
5959
end
6060

6161
assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
62-
assert_equal [key_2, key_1], events[0].payload[:key]
63-
assert_equal [key_1], events[0].payload[:hits]
62+
assert_equal [normalized_key(key_2), normalized_key(key_1)], events[0].payload[:key]
63+
assert_equal [normalized_key(key_1)], events[0].payload[:hits]
64+
assert_equal @cache.class.name, events[0].payload[:store]
65+
end
66+
67+
def test_increment_instrumentation
68+
key_1 = SecureRandom.uuid
69+
@cache.write(key_1, 0)
70+
71+
events = with_instrumentation "increment" do
72+
@cache.increment(key_1)
73+
end
74+
75+
assert_equal %w[ cache_increment.active_support ], events.map(&:name)
76+
assert_equal normalized_key(key_1), events[0].payload[:key]
77+
assert_equal @cache.class.name, events[0].payload[:store]
78+
end
79+
80+
81+
def test_decrement_instrumentation
82+
key_1 = SecureRandom.uuid
83+
@cache.write(key_1, 0)
84+
85+
events = with_instrumentation "decrement" do
86+
@cache.decrement(key_1)
87+
end
88+
89+
assert_equal %w[ cache_decrement.active_support ], events.map(&:name)
90+
assert_equal normalized_key(key_1), events[0].payload[:key]
6491
assert_equal @cache.class.name, events[0].payload[:store]
6592
end
6693

@@ -75,4 +102,8 @@ def with_instrumentation(method)
75102
ensure
76103
ActiveSupport::Notifications.unsubscribe event_name
77104
end
105+
106+
def normalized_key(key)
107+
@cache.send(:normalize_key, key, @cache.options)
108+
end
78109
end

guides/source/active_support_instrumentation.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,6 @@ Cache stores may add their own data as well.
599599

600600
#### `cache_increment.active_support`
601601

602-
This event is only emitted when using [`MemCacheStore`][ActiveSupport::Cache::MemCacheStore]
603-
or [`RedisCacheStore`][ActiveSupport::Cache::RedisCacheStore].
604-
605602
| Key | Value |
606603
| --------- | ----------------------- |
607604
| `:key` | Key used in the store |
@@ -618,8 +615,6 @@ or [`RedisCacheStore`][ActiveSupport::Cache::RedisCacheStore].
618615

619616
#### `cache_decrement.active_support`
620617

621-
This event is only emitted when using the Memcached or Redis cache stores.
622-
623618
| Key | Value |
624619
| --------- | ----------------------- |
625620
| `:key` | Key used in the store |

0 commit comments

Comments
 (0)