Skip to content

Commit

Permalink
Support expiration using ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
heka1024 committed Jan 26, 2024
1 parent cc7758b commit da43474
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Kredis::Types::List < Kredis::Types::Proxying
prepend Kredis::DefaultValues

proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :expire
proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :expire, :ttl

attr_accessor :typed, :expires_in

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

lpush types_to_strings(elements, typed)
expire expires_in.to_i, nx: true if expires_in
elements
with_expiration do
lpush types_to_strings(elements, typed)
end
end

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

rpush types_to_strings(elements, typed)
expire expires_in.to_i, nx: true if expires_in
elements

with_expiration do
rpush types_to_strings(elements, typed)
end
end
alias << append

Expand All @@ -45,4 +46,12 @@ def last(n = nil)
def set_default
append default
end

def with_expiration(&block)
block.call.tap do
if expires_in && ttl < 0
expire expires_in.to_i
end
end
end
end

0 comments on commit da43474

Please sign in to comment.