Skip to content
Permalink
Browse files
MemCacheStore: Support expiring counters
Support `expires_in` in `ActiveSupport::Cache::MemCacheStore#increment`
and `#decrement`.

Closes #30716.
  • Loading branch information
aeroastro authored and jeremy committed Nov 20, 2017
1 parent ae7593e commit b22ee64
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
@@ -1,3 +1,16 @@
* MemCacheStore: Support expiring counters.

Pass `expires_in: [seconds]` to `#increment` and `#decrement` options
to set the Memcached TTL (time-to-live) if the counter doesn't exist.
If the counter exists, Memcached doesn't extend its expiry when it's
incremented or decremented.

```
Rails.cache.increment("my_counter", 1, expires_in: 2.minutes)
```

*Takumasa Ochi*

* Handle `TZInfo::AmbiguousTime` errors

Make `ActiveSupport::TimeWithZone` match Ruby's handling of ambiguous
@@ -122,7 +122,7 @@ def increment(name, amount = 1, options = nil)
options = merged_options(options)
instrument(:increment, name, amount: amount) do
rescue_error_with nil do
@data.incr(normalize_key(name, options), amount)
@data.incr(normalize_key(name, options), amount, options[:expires_in])
end
end
end
@@ -135,7 +135,7 @@ def decrement(name, amount = 1, options = nil)
options = merged_options(options)
instrument(:decrement, name, amount: amount) do
rescue_error_with nil do
@data.decr(normalize_key(name, options), amount)
@data.decr(normalize_key(name, options), amount, options[:expires_in])
end
end
end
@@ -57,6 +57,22 @@ def test_local_cache_raw_values
end
end

def test_increment_expires_in
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, raw: true)
cache.clear
assert_called_with cache.instance_variable_get(:@data), :incr, [ "foo", 1, 60 ] do
cache.increment("foo", 1, expires_in: 60)
end
end

def test_decrement_expires_in
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, raw: true)
cache.clear
assert_called_with cache.instance_variable_get(:@data), :decr, [ "foo", 1, 60 ] do
cache.decrement("foo", 1, expires_in: 60)
end
end

def test_local_cache_raw_values_with_marshal
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, raw: true)
cache.clear

0 comments on commit b22ee64

Please sign in to comment.