diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 8d8bc7f8c3fdd..28654c590bbee 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -58,7 +58,9 @@ def cleanup(options = nil) # cache.increment("baz") # => 6 # def increment(name, amount = 1, options = nil) - modify_value(name, amount, options) + instrument(:increment, normalize_key(name, options), amount: amount) do + modify_value(name, amount, options) + end end # Decrement a cached integer value. Returns the updated value. @@ -73,7 +75,9 @@ def increment(name, amount = 1, options = nil) # cache.decrement("baz") # => 4 # def decrement(name, amount = 1, options = nil) - modify_value(name, -amount, options) + instrument(:decrement, normalize_key(name, options), amount: amount) do + modify_value(name, -amount, options) + end end def delete_matched(matcher, options = nil) @@ -210,8 +214,6 @@ def search_dir(dir, &callback) # Modifies the amount of an integer value that is stored in the cache. # If the key is not found it is created and set to +amount+. def modify_value(name, amount, options) - options = merged_options(options) - key = normalize_key(name, options) version = normalize_version(name, options) amount = Integer(amount) diff --git a/activesupport/lib/active_support/cache/memory_store.rb b/activesupport/lib/active_support/cache/memory_store.rb index 9d727258e0bef..be788b85ae606 100644 --- a/activesupport/lib/active_support/cache/memory_store.rb +++ b/activesupport/lib/active_support/cache/memory_store.rb @@ -147,7 +147,10 @@ def pruning? # cache.increment("baz") # => 6 # def increment(name, amount = 1, options = nil) - instrument(:increment, name, amount: amount) do + options = merged_options(options) + key = normalize_key(name, options) + + instrument(:increment, key, amount: amount) do modify_value(name, amount, options) end end @@ -164,7 +167,10 @@ def increment(name, amount = 1, options = nil) # cache.decrement("baz") # => 4 # def decrement(name, amount = 1, options = nil) - instrument(:decrement, name, amount: amount) do + options = merged_options(options) + key = normalize_key(name, options) + + instrument(:decrement, key, amount: amount) do modify_value(name, -amount, options) end end @@ -239,7 +245,6 @@ def delete_entry(key, **options) # Modifies the amount of an integer value that is stored in the cache. # If the key is not found it is created and set to +amount+. def modify_value(name, amount, options) - options = merged_options(options) key = normalize_key(name, options) version = normalize_version(name, options) diff --git a/activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb b/activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb index a25bea26bbf82..fa916f772b94e 100644 --- a/activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb +++ b/activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb @@ -64,6 +64,33 @@ def test_read_multi_instrumentation assert_equal @cache.class.name, events[0].payload[:store] end + def test_increment_instrumentation + key_1 = SecureRandom.uuid + @cache.write(key_1, 0) + + events = with_instrumentation "increment" do + @cache.increment(key_1) + end + + assert_equal %w[ cache_increment.active_support ], events.map(&:name) + assert_equal key_1, events[0].payload[:key] + assert_equal @cache.class.name, events[0].payload[:store] + end + + + def test_decrement_instrumentation + key_1 = SecureRandom.uuid + @cache.write(key_1, 0) + + events = with_instrumentation "decrement" do + @cache.decrement(key_1) + end + + assert_equal %w[ cache_decrement.active_support ], events.map(&:name) + assert_equal key_1, events[0].payload[:key] + assert_equal @cache.class.name, events[0].payload[:store] + end + private def with_instrumentation(method) event_name = "cache_#{method}.active_support" diff --git a/guides/source/active_support_instrumentation.md b/guides/source/active_support_instrumentation.md index 507feaa339414..dd27dbd03fe1e 100644 --- a/guides/source/active_support_instrumentation.md +++ b/guides/source/active_support_instrumentation.md @@ -599,9 +599,6 @@ Cache stores may add their own data as well. #### `cache_increment.active_support` -This event is only emitted when using [`MemCacheStore`][ActiveSupport::Cache::MemCacheStore] -or [`RedisCacheStore`][ActiveSupport::Cache::RedisCacheStore]. - | Key | Value | | --------- | ----------------------- | | `:key` | Key used in the store | @@ -618,8 +615,6 @@ or [`RedisCacheStore`][ActiveSupport::Cache::RedisCacheStore]. #### `cache_decrement.active_support` -This event is only emitted when using the Memcached or Redis cache stores. - | Key | Value | | --------- | ----------------------- | | `:key` | Key used in the store |