Skip to content

Support expires_in for list #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/kredis/types.rb
Original file line number Diff line number Diff line change
@@ -61,8 +61,8 @@ def hash(key, typed: :string, default: nil, config: :shared, after_change: nil)
type_from(Hash, config, key, after_change: after_change, default: default, typed: typed)
end

def list(key, default: nil, typed: :string, config: :shared, after_change: nil)
type_from(List, config, key, after_change: after_change, default: default, typed: typed)
def list(key, default: nil, typed: :string, config: :shared, after_change: nil, expires_in: nil)
type_from(List, config, key, after_change: after_change, default: default, typed: typed, expires_in: expires_in)
end

def unique_list(key, default: nil, typed: :string, limit: nil, config: :shared, after_change: nil)
25 changes: 21 additions & 4 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
@@ -3,9 +3,9 @@
class Kredis::Types::List < Kredis::Types::Proxying
prepend Kredis::DefaultValues

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

attr_accessor :typed
attr_accessor :typed, :expires_in

def elements
strings_to_types(lrange(0, -1) || [], typed)
@@ -17,11 +17,20 @@ def remove(*elements)
end

def prepend(*elements)
lpush types_to_strings(elements, typed) if elements.flatten.any?
return if elements.flatten.empty?

with_expiration do
lpush types_to_strings(elements, typed)
end
end

def append(*elements)
rpush types_to_strings(elements, typed) if elements.flatten.any?
return if elements.flatten.empty?


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

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

def with_expiration(&block)
result = block.call
if expires_in && ttl < 0
expire expires_in.to_i
end
result
end
end
27 changes: 27 additions & 0 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
@@ -76,6 +76,33 @@ class ListTest < ActiveSupport::TestCase
assert_equal %w[ 2 3 ], @list.elements
end

test "append with expiring list" do
@list = Kredis.list "mylist", expires_in: 1.second
@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
assert_equal [], @list.elements
end

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

sleep 0.2.seconds
@list.prepend(3)

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

sleep 0.6.seconds
assert_equal [], @list.elements
end

test "default" do
@list = Kredis.list "mylist", default: %w[ 1 2 3 ]