Skip to content

Commit a4c2838

Browse files
committed
Support expiration using ttl
1 parent d425b31 commit a4c2838

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/kredis/types/list.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class Kredis::Types::List < Kredis::Types::Proxying
44
prepend Kredis::DefaultValues
55

6-
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :expire
6+
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :expire, :ttl
77

88
attr_accessor :typed, :expires_in
99

@@ -19,17 +19,18 @@ def remove(*elements)
1919
def prepend(*elements)
2020
return if elements.flatten.empty?
2121

22-
lpush types_to_strings(elements, typed)
23-
expire expires_in.to_i, nx: true if expires_in
24-
elements
22+
with_expiration do
23+
lpush types_to_strings(elements, typed)
24+
end
2525
end
2626

2727
def append(*elements)
2828
return if elements.flatten.empty?
2929

30-
rpush types_to_strings(elements, typed)
31-
expire expires_in.to_i, nx: true if expires_in
32-
elements
30+
31+
with_expiration do
32+
rpush types_to_strings(elements, typed)
33+
end
3334
end
3435
alias << append
3536

@@ -45,4 +46,12 @@ def last(n = nil)
4546
def set_default
4647
append default
4748
end
49+
50+
def with_expiration(&block)
51+
block.call.tap do
52+
if expires_in && ttl < 0
53+
expire expires_in.to_i
54+
end
55+
end
56+
end
4857
end

0 commit comments

Comments
 (0)