Skip to content
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

Support expiration time for lists #133

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions lib/kredis/types/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

attr_accessor :typed
attr_accessor :typed, :expires_in

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

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

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

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

rpush types_to_strings(elements, typed)
expire expires_in.to_i, nx: true if expires_in
elements
end
alias << append

Expand Down
27 changes: 27 additions & 0 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand Down
Loading