Skip to content

Commit

Permalink
increment/decrement in litecache now return the new value of the targ…
Browse files Browse the repository at this point in the history
…et key, fixes #122
  • Loading branch information
oldmoe committed Jul 11, 2024
1 parent b896fa8 commit 86af0e0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
8 changes: 3 additions & 5 deletions lib/active_support/cache/litecache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/litestack/litecache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions lib/litestack/sql/litecache.sql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions test/test_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions test/test_cache_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 86af0e0

Please sign in to comment.