Milkncookiez
Milkncookiez

Reputation: 7377

Get hash sets from a given one - onwards

I am storing hash sets with the following keys: measurement:<current_timestamp>, f.e. measurement:1482236501103, and then at a later point I would like to pass a certain timestamp and get all the records from the given timestamp till the end. But since Redis doesn't order the records, the only solution I can come up with is to check each key - whether the timestamp from its name is "bigger" than the given one.

Is there a better way?

Upvotes: 2

Views: 82

Answers (2)

Milkncookiez
Milkncookiez

Reputation: 7377

I did something similar to what @Itamar Haber suggested. I did utilize the Sorted Sets and used the timestamp as score, but since the values that I'm storing are stringified JSON objects, I simply added the timestamp as another property in the object (to make the values unique).

And, additionally, to be able to get all records from given timestamp to last record, I added a key "measurement:last" that will just hold the timestamp of the lastly input record in the Sorted Set. This way I can use ZRANGEBYSCORE and provide the given timestamp as start and the last one as end.

Upvotes: 0

Itamar Haber
Itamar Haber

Reputation: 49932

Indeed, there is a better way to serve that type of query.

Store the measurements in a Sorted Set - keep the timestamp in the score and the element itself is the measured value. Because members in a set must be unique but I assume that measurement value can be repeated, instead of storing the value as is prefix it with the timestamp as well to make the member unique. That means that if at time 123 you measured the value 987, you'll store it in Redis with the following command:

ZADD measurements 123 123:987

To perform the query, use the ZRANGEBYSCORE and process the reply in your client application to extract the value from the concatenated timestamp:value members.

Upvotes: 2

Related Questions