Skip to content
This repository was archived by the owner on Jan 22, 2022. It is now read-only.

Commit a794a31

Browse files
fanjieqitubbo
authored andcommitted
Add :expires_in support to increment and decrement (#120)
Keeps feature parity between **redis-activesupport** and `ActiveSupport::Cache::RedisCacheStore`.
1 parent f79edd9 commit a794a31

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

lib/active_support/cache/redis_store.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,17 @@ def fetch_multi(*names)
187187
#
188188
# cache.increment "rabbit"
189189
# cache.read "rabbit", :raw => true # => "1"
190-
def increment(key, amount = 1, options = {})
191-
options = merged_options(options)
192-
instrument(:increment, key, :amount => amount) do
193-
failsafe(:increment) do
194-
with{|c| c.incrby normalize_key(key, options), amount}
190+
def increment(name, amount = 1, options = {})
191+
instrument :increment, name, amount: amount do
192+
failsafe :increment do
193+
options = merged_options(options)
194+
key = normalize_key(name, options)
195+
196+
with do |c|
197+
c.incrby(key, amount).tap do
198+
write_key_expiry(c, key, options)
199+
end
200+
end
195201
end
196202
end
197203
end
@@ -217,11 +223,17 @@ def increment(key, amount = 1, options = {})
217223
#
218224
# cache.decrement "rabbit"
219225
# cache.read "rabbit", :raw => true # => "-1"
220-
def decrement(key, amount = 1, options = {})
221-
options = merged_options(options)
222-
instrument(:decrement, key, :amount => amount) do
223-
failsafe(:decrement) do
224-
with{|c| c.decrby normalize_key(key, options), amount}
226+
def decrement(name, amount = 1, options = {})
227+
instrument :decrement, name, amount: amount do
228+
failsafe :decrement do
229+
options = merged_options(options)
230+
key = normalize_key(name, options)
231+
232+
with do |c|
233+
c.decrby(key, amount).tap do
234+
write_key_expiry(c, key, options)
235+
end
236+
end
225237
end
226238
end
227239
end
@@ -279,6 +291,12 @@ def read_entry(key, options)
279291
end
280292
end
281293

294+
def write_key_expiry(client, key, options)
295+
if options[:expires_in] && client.ttl(key) < 0
296+
client.expire key, options[:expires_in].to_i
297+
end
298+
end
299+
282300
##
283301
# Implement the ActiveSupport::Cache#delete_entry
284302
#

test/active_support/cache/redis_store_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,24 @@ def setup
335335
end
336336
end
337337

338+
it "increments a key with expiration time" do
339+
with_store_management do |store|
340+
store.increment "counter", 1, :expires_in => 1.second
341+
store.read("counter", :raw => true).to_i.must_equal(1)
342+
sleep 2
343+
store.read("counter", :raw => true).must_be_nil
344+
end
345+
end
346+
347+
it "decrements a key with expiration time" do
348+
with_store_management do |store|
349+
store.decrement "counter", 1, :expires_in => 1.second
350+
store.read("counter", :raw => true).to_i.must_equal(-1)
351+
sleep 2
352+
store.read("counter", :raw => true).must_be_nil
353+
end
354+
end
355+
338356
it "clears the store" do
339357
with_store_management do |store|
340358
store.clear

0 commit comments

Comments
 (0)