aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Lütke <tobi@jadedpixel.com>2008-04-29 15:12:47 -0400
committerTobias Lütke <tobi@jadedpixel.com>2008-04-29 15:12:47 -0400
commitfef82759ff97692470496905951882a0aab49d5b (patch)
tree7acd6eb8ac07f96523ac3d99024e6a2099059d9b
parent9f07b1edcd2955dc7af166c422309da55372a92c (diff)
downloadrails-fef82759ff97692470496905951882a0aab49d5b.tar.gz
rails-fef82759ff97692470496905951882a0aab49d5b.tar.bz2
rails-fef82759ff97692470496905951882a0aab49d5b.zip
Implement increment/decrement on cache storage engines, using read/write by default and using atomic command on memcache
-rw-r--r--actionpack/test/controller/session/mem_cache_store_test.rb5
-rw-r--r--activesupport/lib/active_support/cache.rb17
-rw-r--r--activesupport/lib/active_support/cache/mem_cache_store.rb18
3 files changed, 37 insertions, 3 deletions
diff --git a/actionpack/test/controller/session/mem_cache_store_test.rb b/actionpack/test/controller/session/mem_cache_store_test.rb
index df48e6d9c5..a7d48431f8 100644
--- a/actionpack/test/controller/session/mem_cache_store_test.rb
+++ b/actionpack/test/controller/session/mem_cache_store_test.rb
@@ -62,9 +62,8 @@ class MemCacheStoreTest < Test::Unit::TestCase
assert_equal d, s.cache.get(session_key)[:test]
assert_equal d, s[:test]
end
- end
-
-
+ end
+
def test_deletion
new_session do |s|
session_key = 'session:' + s.session_id
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 64394afec4..463cba6cf0 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -87,8 +87,25 @@ module ActiveSupport
def delete_matched(matcher, options = nil)
log("delete matched", matcher.inspect, options)
+ end
+
+ def increment(key, amount = 1)
+ log("incrementing", key, amount)
+ if num = read(key)
+ write(key, num + amount)
+ else
+ nil
+ end
end
+ def decrement(key, amount = 1)
+ log("decrementing", key, amount)
+ if num = read(key)
+ write(key, num - amount)
+ else
+ nil
+ end
+ end
private
def log(operation, key, options)
diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb
index ad220a9c2c..15e2df13b3 100644
--- a/activesupport/lib/active_support/cache/mem_cache_store.rb
+++ b/activesupport/lib/active_support/cache/mem_cache_store.rb
@@ -48,8 +48,26 @@ module ActiveSupport
rescue MemCache::MemCacheError => e
logger.error("MemCacheError (#{e}): #{e.message}")
false
+ end
+
+ def increment(key, amount = 1)
+ log("incrementing", key, amount)
+
+ response = @data.incr(key, amount)
+ response == Response::NOT_FOUND ? nil : response
+ rescue MemCache::MemCacheError
+ nil
end
+ def decrement(key, amount = 1)
+ log("decrement", key, amount)
+
+ response = data.decr(key, amount)
+ response == Response::NOT_FOUND ? nil : response
+ rescue MemCache::MemCacheError
+ nil
+ end
+
def delete_matched(matcher, options = nil)
super
raise "Not supported by Memcache"