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

Retrieving elements from ordered set that were updated in given time interval #154

Open
janko opened this issue Aug 29, 2024 · 1 comment

Comments

@janko
Copy link

janko commented Aug 29, 2024

We use an ordered set for registering heartbeats of devices connected to our backend via websockets. Discovering that Kredis stores scores as float timestamps, I realized I could use ZRANGE to retrieve device IDs that have either recent or stale heartbeat:

heartbeats = Kredis.ordered_set("devices:heartbeats")
heartbeats.zrange("-inf", 1.minute.ago.to_f, byscore: true) # returns device IDs with stale heartbeat
heartbeats.zrange(1.minute.ago.to_f, "+inf", byscore: true) # returns device IDs with recent heartbeat

I recently created a helper method that accepts a time range to make this easier:

def heartbeat_ids(range)
  from = range.begin&.to_f || "-inf"
  to = range.end&.to_f || "+inf"

  heartbeats = Kredis.ordered_set("devices:heartbeats")
  heartbeats.zrange(from, to, byscore: true)
end
heartbeat_ids(...1.minute.ago) # stale heartbeats
heartbeat_ids(1.minute.ago..) # recent heartbeats

I was wondering whether there is interest in adding a similar method to Kredis::OrderedSet, or make #elements accept an optional time range.

@janko janko changed the title Retrieving elements from ordered set that have updated in given time interval Retrieving elements from ordered set that were updated in given time interval Aug 29, 2024
@maxim
Copy link

maxim commented Oct 28, 2024

@janko Looks convenient and appropriate for this type in my view.

Half-relatedly, I wonder if Basecamp is still using Redis and Kredis internally, and whether this gem still makes sense for their team to maintain? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants