Skip to content

Commit 86af0e0

Browse files
committed
increment/decrement in litecache now return the new value of the target key, fixes #122
1 parent b896fa8 commit 86af0e0

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

lib/active_support/cache/litecache.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ def initialize(options = {})
2424
def increment(key, amount = 1, options = nil)
2525
key = key.to_s
2626
options = merged_options(options)
27-
2827
@cache.transaction do
2928
if (value = read(key, options))
30-
value = value.to_i + amount
31-
write(key, value, options)
32-
else
33-
write(key, amount, options)
29+
amount += value.to_i
3430
end
31+
write(key, amount, options)
3532
end
33+
amount
3634
end
3735

3836
def decrement(key, amount = 1, options = nil)

lib/litestack/litecache.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def delete(key)
164164
# increment an integer value by amount, optionally add an expiry value (in seconds)
165165
def increment(key, amount = 1, expires_in = nil)
166166
expires_in ||= @expires_in
167-
@conn.acquire { |cache| cache.stmts[:incrementer].execute!(key.to_s, amount, expires_in) }
167+
@conn.acquire { |cache| cache.stmts[:incrementer].execute!(key.to_s, amount, expires_in)[0][0] }
168168
end
169169

170170
# decrement an integer value by amount, optionally add an expiry value (in seconds)

lib/litestack/sql/litecache.sql.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ stmts:
7777
SET
7878
value = CAST(value AS int) + CAST(EXCLUDED.value AS int),
7979
last_used = EXCLUDED.last_used,
80-
expires_in = EXCLUDED.expires_in;
81-
82-
80+
expires_in = EXCLUDED.expires_in
81+
RETURNING value;
8382
8483
counter: >
8584
SELECT count(*) FROM data;

test/test_cache.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ def test_cache_expiry
4444
end
4545

4646
def test_increment_decrement
47-
@cache.increment("key")
47+
res = @cache.increment("key")
48+
assert_equal 1, res
4849
assert_equal 1, @cache.get("key")
49-
@cache.increment("key", 5)
50+
res = @cache.increment("key", 5)
51+
assert_equal 6, res
5052
assert_equal 6, @cache.get("key")
51-
@cache.decrement("key", 4)
53+
res = @cache.decrement("key", 4)
54+
assert_equal 2, res
5255
assert_equal 2, @cache.get("key")
5356
end
5457

test/test_cache_rails.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ def test_cache_expiry
5151
end
5252

5353
def test_increment_decrement
54-
@cache.increment("key")
54+
res = @cache.increment("key")
55+
assert_equal 1, res
5556
assert_equal 1, @cache.read("key")
56-
@cache.increment("key", 5)
57+
res = @cache.increment("key", 5)
58+
assert_equal 6, res
5759
assert_equal 6, @cache.read("key")
58-
@cache.decrement("key", 4)
60+
res = @cache.decrement("key", 4)
61+
assert_equal 2, res
5962
assert_equal 2, @cache.read("key")
6063
end
6164

0 commit comments

Comments
 (0)