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

Cannot set expiration time for lists #129

Open
leoncruz opened this issue Sep 20, 2023 · 3 comments · May be fixed by #142
Open

Cannot set expiration time for lists #129

leoncruz opened this issue Sep 20, 2023 · 3 comments · May be fixed by #142

Comments

@leoncruz
Copy link

I'm creating a list of items with an expiration time, but, I'm having the following error:

irb>> Kredis.list "mylist", expires_in: 1.minute
kredis-1.5.0/lib/kredis/types.rb:62:in `list': unknown keyword: :expires_in (ArgumentError)
@nzwsch
Copy link

nzwsch commented Oct 14, 2023

https://github.com/rails/kredis/blob/main/lib/kredis/types.rb#L64-L66

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)
end

It appears that some kredis types do not support expires_in.
If this is not simply a typo, there may be a reason for this.

@leoncruz
Copy link
Author

@nzwsch Yes, I have to do a little trick to expire work:

  1. First add the parameter:
module Kredis::Types
  def list(key, typed: :string, config: :shared, after_change: nil, expires_in: nil)
    type_from(List, config, key, after_change: after_change, typed: typed, expires_in: expires_in)
  end

  def unique_list(key, typed: :string, limit: nil, config: :shared, after_change: nil, expires_in: nil)
    type_from(UniqueList, config, key, after_change: after_change, typed: typed, limit: limit, expires_in: expires_in)
  end
end
  1. Override the Kredis list to set the expiration time on key when first value is set:
class Kredis::Types::List < Kredis::Types::Proxying
  proxying :lrange, :lrem, :lpush, :ltrim, :rpush, :exists?, :del, :expire

  attr_accessor :typed, :expires_in

  def prepend(*elements)
    return unless elements.flatten.any?

    l = lpush types_to_strings(elements, typed)

    set_expire

    l
  end

  def append(*elements)
    return unless elements.flatten.any?

    r = rpush types_to_strings(elements, typed)

    set_expire

    r
  end
  alias << append

  private

  attr_accessor :expire_at

  def set_expire
    return unless expires_in

    if expired?
      @expire_at = expires_in.from_now

      expire expires_in
    end
  end

  def expired?
    !expire_at || expire_at.past?
  end
end

I have to do the same with hash type too. Would be nice to have this behavior in the Kredis itself

@jeremy
Copy link
Member

jeremy commented Oct 23, 2023

Pull request welcome 🤗

@heka1024 heka1024 linked a pull request Jan 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants