Peter
Peter

Reputation: 759

Redis Sets - date added

I currently have a Redis Set and I'm not sure if this is possible but thought I'd ask just in case someone knows of a way around it. Is it possible to add a date of when a new element for a key is created when using 'SADD' command?

I've tried searching the web but I can't find anything, which tells me its not possible. Is there another way of doing this? Something like the below and then being able to get the date the element was created?

SADD my:key:string "1000", Time.now

Upvotes: 0

Views: 2079

Answers (2)

Pascal Le Merrer
Pascal Le Merrer

Reputation: 5981

Use a Sorted Set instead of a Set. Using the timestamp as a score will allow you to retrieve your values ordered by date, values for a given date, values after of before a given date, etc. Or of course just the date for the value.

ZADD myzset 12345678 "my value"

Upvotes: 2

Chhavi Gangwal
Chhavi Gangwal

Reputation: 1176

In order to set the date of an element save the element in form of json :

SADD mset "{value:1000,created:18-03-2014}"

So when you retrieve the element its creation date will be also be retrieved.

Upvotes: 0

Related Questions