提交 fef82759 编写于 作者: T Tobias Lütke

Implement increment/decrement on cache storage engines, using read/write by...

Implement increment/decrement on cache storage engines, using read/write by default and using atomic command on memcache
上级 9f07b1ed
......@@ -62,9 +62,8 @@ def test_storage
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
......
......@@ -87,8 +87,25 @@ def delete(key, options = nil)
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)
......
......@@ -48,8 +48,26 @@ def delete(key, options = nil)
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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册