I'm new to Redis, and as far as I can tell, it seems like I'm looking 
for something like what SADDEX or SETNXEX would be. 
A way to add an item, only if it does not exist, and have it use the expiry feature without having to have logic on the client and another call to the database. I've seen the posts about SETNXEX and how adding a feature that is a simple combination of other features has a low chance of being added but... that sure is disappointing! It sure seems like some existing commands are syntactic sugar over combinations of others. This one seems like a great combination.
I've seen reference to writing a Lua script to do this. Are there any examples that you can point me to? I'm imagining that it is sort of like a stored procedure from the relational world? So that the client would make one call to the database, and the Lua script would do both the SETNX and EXPIRE commands during that call?
Thanks, Travis
Travis Laborde's gravatar image asked Sep 21 2011 at 09:26 in Redis-Db by Travis Laborde

4 Answers

People have asked to be able to expire individual items inside hashes, sets, zsets, and lists (all of the containers, btw), and the answer is always the same: no. The actual method by which you could do the same thing is...
ZADD my-expire <timestamp> <key>_<item>
But that sucks up quite a bit of memory for the items that have not yet expired, and you need to manually expire items. If your number of items to be expired is relatively small at any point in time, and you are okay manually expiring items, you can consume the above zset, expiring items as necessary.
Regards, - Josiah
Josiah Carlson's gravatar image answered Sep 21 2011 at 13:46 by Josiah Carlson
that also does not work because it risks collision (one zadd overriding another!)
On Sep 21, 1:39pm, Josiah Carlson wrote: > People have asked to be able to expire individual items inside hashes, > sets, zsets, and lists (all of the containers, btw), and the answer is > always the same: no. > > The actual method by which you could do the same thing is... > > ZADD my-expire <timestamp> <key>_<item> > > But that sucks up quite a bit of memory for the items that have not > yet expired, and you need to manually expire items. If your number of > items to be expired is relatively small at any point in time, and you > are okay manually expiring items, you can consume the above zset, > expiring items as necessary. > > Regards, > - Josiah > > On Wed, Sep 21, 2011 at 6:19 PM, Travis Laborde > > > > > > >
Aaron Blohowiak's gravatar image answered Sep 21 2011 at 20:55 by Aaron Blohowiak
Thanks guys. I'm still a Redis noob and already I'm getting the "no" answer :)
Travis
On Sep 21, 11:48pm, Aaron Blohowiak wrote:
Travis Laborde's gravatar image answered Sep 22 2011 at 03:36 by Travis Laborde
Only in the case of lists (in which case you could remove *all* instances of the item that are in the list), the rest would update the expire time of a particular item (as long as you are careful with the <key>_<member> construction in the zset). Regards, - Josiah
Josiah Carlson's gravatar image answered Sep 22 2011 at 12:26 by Josiah Carlson

Related Discussions