aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTakumasa Ochi <aeroastro@users.noreply.github.com>2017-11-19 03:50:59 +0900
committerJeremy Daer <jeremydaer@gmail.com>2017-11-20 14:49:11 -0800
commitb22ee64b5b30c6d5039c292235e10b24b1057f6d (patch)
treebead1307088117c6c95269f261fdb75242f8ca54
parentae7593e7e842ec5efb8d58a7ce005ba55fd4c886 (diff)
downloadrails-b22ee64b5b30c6d5039c292235e10b24b1057f6d.tar.gz
rails-b22ee64b5b30c6d5039c292235e10b24b1057f6d.tar.bz2
rails-b22ee64b5b30c6d5039c292235e10b24b1057f6d.zip
MemCacheStore: Support expiring counters
Support `expires_in` in `ActiveSupport::Cache::MemCacheStore#increment` and `#decrement`. Closes #30716.
-rw-r--r--activesupport/CHANGELOG.md13
-rw-r--r--activesupport/lib/active_support/cache/mem_cache_store.rb4
-rw-r--r--activesupport/test/cache/stores/mem_cache_store_test.rb16
3 files changed, 31 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 904dab0e05..88bbafc3a8 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -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
diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb
index 9242a334f2..50f072388d 100644
--- a/activesupport/lib/active_support/cache/mem_cache_store.rb
+++ b/activesupport/lib/active_support/cache/mem_cache_store.rb
@@ -122,7 +122,7 @@ module ActiveSupport
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 @@ module ActiveSupport
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
diff --git a/activesupport/test/cache/stores/mem_cache_store_test.rb b/activesupport/test/cache/stores/mem_cache_store_test.rb
index 1b73fb65eb..99624caf8a 100644
--- a/activesupport/test/cache/stores/mem_cache_store_test.rb
+++ b/activesupport/test/cache/stores/mem_cache_store_test.rb
@@ -57,6 +57,22 @@ class MemCacheStoreTest < ActiveSupport::TestCase
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