简体   繁体   中英

How do you expire a field of a hash in Redis?

I'd like to expires certain field of a hash after different amount of time. From the documentation it doesn't look to be possible: http://redis.io/commands/expire . Can I emulate this behavior somehow? Currently I'm just copying the hash name to be a prefix of all of its fields and adding expires to each. For the structure I'd like to store is:

"PEOPLE" : {
 "NICK" : 29
 "AMANDA" : 23
}

but I can't set an expire on "NICK", only on the whole hash "PEOPLE". So I convert it to and number of key, values:

"PEOPLE_NICK" : 29 (expire 2 hours)
"PEOPLE_AMANDA" 23 (expire 1 hour)

but there are many keys and it makes it very difficult to manage and more tedious to fetch as I have to fetch everything by the common prefix.

If the above example is an accurate representation, and depending on how exactly you'll consume the data, you could use a sorted set with the score of each person be a Unix timestamp. Then you'd use zremrangebyscore on an Cron job to clean out items which are beyond you acceptable window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM