0

I am using a stream and that stream stores data with a timestamp as key, to read it back 10 seconds later (this I am using to send push notifications). I was wondering if I could query redis for a list of timestamps in a specific range.

For example: I have a thread running that will get that data from redis every 10 seconds. Let's say there are 7 new messages placed, that means after 10 seconds I'd like to send 7 new push notifications for the messages that were placed in that time (I will check if they are already read, but that's another case).

Just wondering, I am not familiar with redis at all so I hope anyone knows. I am using this

1 Answer 1

3

You could use a sorted set, in which the score of each member would be the unix timestamp.

To store a new message, use ZADD:

ZADD myzset <timestamp> <data>

Then to get the list of the messages stored since a given time, use ZRANGEBYSCORE:

# return the messages posted since reference_timestamp
ZRANGEBYSCORE myzset <reference_timestamp> +inf

You can delete the messages after they have been sent, using ZREMRANGEBYSCORE

# delete the messages older than reference_timestamp (including those at timestamp)
ZREMRANGEBYSCORE myzset -inf <reference_timestamp> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is exactly what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.