touching keys to extend ttl

3,114 views
Skip to first unread message

Jason Toy

unread,
Aug 13, 2011, 12:55:28 PM8/13/11
to Redis DB
Can I have redis automatically extend the ttl of a key with any
function any time I access it without explicitly telling the key to
extend with the expire command?

David Turnbull

unread,
Aug 13, 2011, 1:21:25 PM8/13/11
to redi...@googlegroups.com
No.  But, check out "maxmemory policy" in the redis.conf.

Salvatore Sanfilippo

unread,
Aug 14, 2011, 5:38:17 AM8/14/11
to redi...@googlegroups.com
Hello Jason,

this is what you need.

MULTI
SOME OP AGAINST KEY
TTL KEY
EXEC

basically send every command as a Redis transaction including the
final TTL command against the key and you are done.

Cheers,
Salvatore

> --
> You received this message because you are subscribed to the Google Groups "Redis DB" group.
> To post to this group, send email to redi...@googlegroups.com.
> To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/redis-db?hl=en.
>
>

--
Salvatore 'antirez' Sanfilippo
open source developer - VMware

http://invece.org
"We are what we repeatedly do. Excellence, therefore, is not an act,
but a habit." -- Aristotele

Marc Gravell

unread,
Aug 14, 2011, 10:06:00 AM8/14/11
to redi...@googlegroups.com
(my notes on versions etc were for the group, not Salvatore - who already knows much better than me :p)

Marc

Marc Gravell

unread,
Aug 14, 2011, 9:59:55 AM8/14/11
to redi...@googlegroups.com, redi...@googlegroups.com
Surely that would be EXPIRE, not TTL?

Also note that the ability to use EXPIRE on a key that already has expiry depends on the redis version.

We use something like this to spoof sliding expiration - although we embed data is the value and handle it in our wrapper layer, reissuing an EXPIRE or SETEX as necessary.

However - I'd happily add a +1 to anybody proposal to include sliding expiry at the server layer.

Marc

On 14 Aug 2011, at 10:38, Salvatore Sanfilippo <ant...@gmail.com> wrote:

Jason Toy

unread,
Aug 15, 2011, 12:43:58 AM8/15/11
to Redis DB
Salvatore, thanks, that is what I'm doing now.

> Hello Jason,
>
> this is what you need.
>
> MULTI
> SOME OP AGAINST KEY
> TTL KEY
> EXEC
>
> basically send every command as a Redis transaction including the
> final TTL command against the key and you are done.
>
> Cheers,
> Salvatore
>
> On Sat, Aug 13, 2011 at 6:55 PM, Jason Toy <jason...@gmail.com> wrote:
> > Can I have redis automatically extend the ttl of a key with any
> > function any time I access it without explicitly telling the key to
> > extend with the expire command?
>
> > --
> > You received this message because you are subscribed to the Google Groups "Redis DB" group.
> > To post to this group, send email to redi...@googlegroups.com.
> > To unsubscribe from this group, send email to redis-db+u...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/redis-db?hl=en.

Xiangrong Fang

unread,
Aug 15, 2011, 3:11:20 AM8/15/11
to redi...@googlegroups.com
Hello Salvatore,

With this command sequence, how long will the expiration of the key be extended?   And what's the benefit of doing this ad-hoc transaction comparing to just issuing an expire command?

Finally I suggest to update the documentation for TTL on redis.io for this technique.

Thanks a lot.

Shannon

2011/8/14 Salvatore Sanfilippo <ant...@gmail.com>

Pieter Noordhuis

unread,
Aug 15, 2011, 3:30:43 AM8/15/11
to redi...@googlegroups.com
Hi Shannon,

The idea is to atomically extend the TTL of the key. As Marc
mentioned, it is not TTL but EXPIRE that should be called with a new
TTL that should overwrite the old one. It is important to wrap this in
a MULTI/EXEC block IFF you want EVERY update to extend the TTL. If
this is not a hard requirement you can just perform the operation and
call EXPIRE without a MULTI/EXEC.

If you find the documentation lacking, I invite you to make the edits
you deem fit in the repository at http://github.com/antirez/redis-doc,
and send a pull request. When necessary, we can continue discussing
the modifications there.

Thanks,
Pieter

Reply all
Reply to author
Forward
0 new messages