From 86af0e02858c73d90cb9b3fa0d0bdaf3e65638a7 Mon Sep 17 00:00:00 2001 From: oldmoe Date: Thu, 11 Jul 2024 14:40:06 +0200 Subject: [PATCH] increment/decrement in litecache now return the new value of the target key, fixes #122 --- lib/active_support/cache/litecache.rb | 8 +++----- lib/litestack/litecache.rb | 2 +- lib/litestack/sql/litecache.sql.yml | 5 ++--- test/test_cache.rb | 9 ++++++--- test/test_cache_rails.rb | 9 ++++++--- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/active_support/cache/litecache.rb b/lib/active_support/cache/litecache.rb index a0fca29..cb2c149 100644 --- a/lib/active_support/cache/litecache.rb +++ b/lib/active_support/cache/litecache.rb @@ -24,15 +24,13 @@ def initialize(options = {}) def increment(key, amount = 1, options = nil) key = key.to_s options = merged_options(options) - @cache.transaction do if (value = read(key, options)) - value = value.to_i + amount - write(key, value, options) - else - write(key, amount, options) + amount += value.to_i end + write(key, amount, options) end + amount end def decrement(key, amount = 1, options = nil) diff --git a/lib/litestack/litecache.rb b/lib/litestack/litecache.rb index 706885c..5ce5657 100644 --- a/lib/litestack/litecache.rb +++ b/lib/litestack/litecache.rb @@ -164,7 +164,7 @@ def delete(key) # increment an integer value by amount, optionally add an expiry value (in seconds) def increment(key, amount = 1, expires_in = nil) expires_in ||= @expires_in - @conn.acquire { |cache| cache.stmts[:incrementer].execute!(key.to_s, amount, expires_in) } + @conn.acquire { |cache| cache.stmts[:incrementer].execute!(key.to_s, amount, expires_in)[0][0] } end # decrement an integer value by amount, optionally add an expiry value (in seconds) diff --git a/lib/litestack/sql/litecache.sql.yml b/lib/litestack/sql/litecache.sql.yml index ba3128b..cb58a41 100644 --- a/lib/litestack/sql/litecache.sql.yml +++ b/lib/litestack/sql/litecache.sql.yml @@ -77,9 +77,8 @@ stmts: SET value = CAST(value AS int) + CAST(EXCLUDED.value AS int), last_used = EXCLUDED.last_used, - expires_in = EXCLUDED.expires_in; - - + expires_in = EXCLUDED.expires_in + RETURNING value; counter: > SELECT count(*) FROM data; diff --git a/test/test_cache.rb b/test/test_cache.rb index 1a0a1a1..7564db8 100644 --- a/test/test_cache.rb +++ b/test/test_cache.rb @@ -44,11 +44,14 @@ def test_cache_expiry end def test_increment_decrement - @cache.increment("key") + res = @cache.increment("key") + assert_equal 1, res assert_equal 1, @cache.get("key") - @cache.increment("key", 5) + res = @cache.increment("key", 5) + assert_equal 6, res assert_equal 6, @cache.get("key") - @cache.decrement("key", 4) + res = @cache.decrement("key", 4) + assert_equal 2, res assert_equal 2, @cache.get("key") end diff --git a/test/test_cache_rails.rb b/test/test_cache_rails.rb index d20bbae..f05b3bf 100644 --- a/test/test_cache_rails.rb +++ b/test/test_cache_rails.rb @@ -51,11 +51,14 @@ def test_cache_expiry end def test_increment_decrement - @cache.increment("key") + res = @cache.increment("key") + assert_equal 1, res assert_equal 1, @cache.read("key") - @cache.increment("key", 5) + res = @cache.increment("key", 5) + assert_equal 6, res assert_equal 6, @cache.read("key") - @cache.decrement("key", 4) + res = @cache.decrement("key", 4) + assert_equal 2, res assert_equal 2, @cache.read("key") end