-
Notifications
You must be signed in to change notification settings - Fork 78
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
Conversation
Hi, @nzwsch sorry for disappear. I have working on code that showed on the #129 and found a problem in my application. The expiration time is always set when a request occours. For example: the first request there is no list, so creates the list, then set the expiration time. On the next request, with existing list, the expiration time is set again. This could be a possible behavior, but is not what I want. I don't know if you found this problem too |
Hey, @nzwsch after some tests I think i found a solution. There is a # lib/kredis/types/list.rb
def prepend(*elements)
return if elements.flatten.empty?
lpush types_to_strings(elements, typed)
expire expires_in.to_i, nx: !reset_expire_on_update if expires_in
end The # lib/kredis/types.rb
def list(key, default: nil, typed: :string, config: :shared, after_change: nil, expires_in: nil, reset_expire_on_update: false)
type_from(List, config, key, after_change: after_change, default: default, typed: typed, expires_in: expires_in, reset_expire_on_update: reset_expire_on_update)
end Would be great if you could test this and add to your PR. |
I looked at the code you presented and did a little implementation, is there any reason why we should support def prepend(*elements)
return if elements.flatten.empty?
lpush types_to_strings(elements, typed)
expire_in expires_in if expires_in && !reset_expire_on_update # This keyword is a little long, I think
elements
end Frankly I am glad you commented on this PR, but I don't think we should expect this PR to be responsive because the maintainers are not. |
@nzwsch The ideia of 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
puts "TTL before insertion: #{Kredis.redis.pttl "mylist"}"
@list.prepend 4 # new item insertion
puts "TTL after insertion: #{Kredis.redis.pttl "mylist"}"
sleep 0.6.seconds
assert_equal [], @list.elements
end Then, the test fails because the list is not empty yet. I print the time to live on test to see that increase after insertion: If use the def expire_in(seconds)
expire seconds.to_i, nx: true
end The test pass and the This is the idea of |
lib/kredis/types/list.rb
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
@leoncruz |
These test failures all seem real. |
@nzwsch I this errors are because the |
@leoncruz |
Closes #129
Since Leon did not respond, I created a PR on his behalf.
I tried to find another way to do it, but there is little change from the code he first wrote.
One thing to note is that the array seemed to be empty if I specified the values for expires_in and default at the same time.
If this PR is accepted, I will look into other Hash, OrderedSet, etc. to see if they can support expires_in as well.