knight
knight

Reputation: 435

Get all keys based on time in Redis

How do I get a list of keys based on time in Redis, or based on a chronological key which is within the dictionary, i.e.:

get foo
{"time":'Thu Aug 15 22:37:37 2013', "data":"etc"}

And I have the "time" key within every one of my Redis entries. How do I fetch a list sorted based on that time, or better, the time when the keys are created?

Upvotes: 1

Views: 3177

Answers (1)

Mike Shultz
Mike Shultz

Reputation: 1418

Redis is a keystore and isn't meant to be operated as you're trying. The 'time' key is irrelevant to redis as this is just a JSON string stored in a Redis string(if I'm understanding correctly). Basically, Redis doesn't care what's in the string, and if it did, it may as well be a full SQL DB.

If you absolutely have to use time, you could include that in the key name. For instance, you want to get all keys in August:

redis 127.0.0.1:6379> MSET foo_20130815223737 "{\"time\":'Thu Aug 15 22:37:37 2013', \"data\":\"etc\"}"  foo_20130816123015 "{\"time\":'Thu Aug 16 12:30:15 2013', \"data\":\"etc\"}"
OK
redis 127.0.0.1:6379> KEYS foo_201308*
1) "foo_20130815223737"
2) "foo_20130816123015"

If you describe why you're trying to do this, I may have a better option. For instance, a Pub/Sub using Redis as a message queue may fit your intentions better.

Upvotes: 1

Related Questions