Skip to content

Commit

Permalink
Documentation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikspang committed Aug 27, 2024
1 parent 4ed775a commit 0a577f2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
10 changes: 6 additions & 4 deletions activesupport/lib/active_support/cache/file_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 8 additions & 3 deletions activesupport/lib/active_support/cache/memory_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions guides/source/active_support_instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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 |
Expand Down

0 comments on commit 0a577f2

Please sign in to comment.