Redis: possible to expire an element in an array or sorted set?

后端 未结 3 554
面向向阳花
面向向阳花 2020-12-02 07:19

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

3条回答
  •  渐次进展
    2020-12-02 08:01

    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

提交回复
热议问题