Redis: possible to expire an element in an array o

2019-01-12 23:11发布

Is it currently only possible to expire an entire key/value pair? What if I want to add values to a List type structure and have them get auto removed 1 hour after insertion. Is that currently possible, or would it require running a cron job to do the purging manually?

标签: caching redis
2条回答
萌系小妹纸
2楼-- · 2019-01-12 23:47

Is it currently only possible to expire an entire key/value pair?

As far as I know, and also according to key commands and document about expiration, currently you can set expiration only to specific key and not to it's underlying data structure. However there is a discussion on google groups about this functionality with outlined alternative solutions.

查看更多
Melony?
3楼-- · 2019-01-12 23:58

There is a common pattern that solves this problem quite well.

Use sorted sets, and use a timestamp as the score. It's then trivial to delete items by score range, which could be done periodically, or only on every write, with reads always ignoring the out of range elements, by reading only a range of scores.

More here: https://groups.google.com/forum/#!topic/redis-db/rXXMCLNkNSs

查看更多
登录 后发表回答