whi
whi

Reputation: 2750

when a redis key expires automatically change value

i'm using hset of redis:

hset(name, k, (level, info, t))

i'd like to automatic set level = -1 if the (name, k) expire after a time.

any command, python is preferred.

Upvotes: 0

Views: 1281

Answers (1)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44102

This cannot be done in reliable manner as expiration does not trigger some action at given moment, but in some later time either during access to that key, or during automatic cleanup procedure.

In practice, for small amount of keys this does not create long delay and you might subscribe to an event, reporting the expiration.

For subscribing to expiration event see related SO answer.

EDIT:

If you would like to modify the value, you would have to run a client, who is subscribed and who reacts to that event by modifying the value.

But due to the nature when it happens (possibly as reaction to attempt to use that key, which was supposed to expire a moment ago), this listening code could come a bit too late. This depends on your real scenario.

Upvotes: 1

Related Questions