Skip to content

Commit

Permalink
Support nx and remove expire_in
Browse files Browse the repository at this point in the history
  • Loading branch information
nzwsch committed Dec 28, 2023
1 parent 3328dda commit 27d3a05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 2 additions & 6 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def prepend(*elements)
return if elements.flatten.empty?

lpush types_to_strings(elements, typed)
expire_in expires_in if expires_in
expire expires_in, nx: true if expires_in
elements
end

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

rpush types_to_strings(elements, typed)
expire_in expires_in if expires_in
expire expires_in, nx: true if expires_in
elements
end
alias << append
Expand All @@ -41,10 +41,6 @@ def last(n = nil)
n ? lrange(-n, -1) : lrange(-1, -1).first
end

def expire_in(seconds)
expire seconds.to_i
end

private
def set_default
append default
Expand Down
16 changes: 11 additions & 5 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ class ListTest < ActiveSupport::TestCase

test "append with expiring list" do
@list = Kredis.list "mylist", expires_in: 1.second
@list.append(%w[1 2 3])

sleep 0.5.seconds
@list.append(%w[1 2])

sleep 0.2.seconds
@list.append(3)

sleep 0.3.seconds
assert_equal %w[ 1 2 3 ], @list.elements

sleep 0.6.seconds
Expand All @@ -89,9 +92,12 @@ class ListTest < ActiveSupport::TestCase

test "prepend with expiring list" do
@list = Kredis.list "mylist", expires_in: 1.second
@list.prepend(%w[1 2 3])
@list.prepend(%w[1 2])

sleep 0.2.seconds
@list.prepend(3)

sleep 0.5.seconds
sleep 0.2.seconds
assert_equal %w[ 3 2 1 ], @list.elements

sleep 0.6.seconds
Expand Down

0 comments on commit 27d3a05

Please sign in to comment.