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 2 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
20 changes: 16 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_in expires_in 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_in expires_in if expires_in
elements
end
alias << append

Expand All @@ -33,6 +41,10 @@ def last(n = nil)
n ? lrange(-n, -1) : lrange(-1, -1).first
end

def expire_in(seconds)
expire seconds.to_i
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we need this. You can just call expire expires_in if expires_in inline.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I borrowed this code from scalar.rb, but it certainly would not have been necessary to define it in a method.

end

private
def set_default
append default
Expand Down
21 changes: 21 additions & 0 deletions test/types/list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ 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 3])

sleep 0.5.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 3])

sleep 0.5.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