集合成员的 TTL [英] TTL for a set member

查看:55
本文介绍了集合成员的 TTL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Redis 是否可以不为特定键设置 TTL(生存时间),而是为集合的成员设置?

Is it possible in Redis to set TTL (time to live) not for a specific key, but for a member for a set?

我正在使用Redis文档提出的标签结构 - 数据是简单的键值对,标签是包含与每个标签对应的键的集合,例如

I am using a structure for tags proposed by Redis documentation - the data are simple key-value pairs, and the tags are sets containing keys corresponding to each tag, e.g.

> SETEX id:id_1 100 'Lorem ipsum'
OK
> SADD tag:tag_1 id:id_1
(integer) 1

id:id_1 将按预期过期,但我没有看到从 tag:tag_1 集中删除相应成员的有效方法.

The key id:id_1 will expire as expected but i don't see an efficient way to remove the corresponding member from the tag:tag_1 set.

我想到的一种方法是使用包含脚本的 cron 作业,该脚本会定期从集合中删除过期的键 - 通过将所有标签名称添加到另一个集合,然后遍历所有标签,然后遍历与每个标签对应的所有 id并检查相应的密钥是否存在 - 如果不存在,则调用 SREM.

One way I came up is using a cron job containing a script which would remove expired keys from sets periodically - by adding all the tag names to another set and then iterating through all the tags, then all the ids corresponding to each tag and checking if corresponding key exists - if not, calling SREM.

我认为这不是一种有效的方法,我可能希望保持标签尽可能干净,因为集合的大小可能会影响多个标签搜索的性能(SINTER).有没有更内部"的方式?

I don't think it will be an efficient way and I would possibly like to keep the tags as clean as possible, because the size of the sets will probably affect performance of searching by multiple tags (SINTER). Is there a more "internal" way?

推荐答案

不能直接使列表、集合或 zsets 中的项目过期.

It is not possible to directly expire items in list, sets, or zsets.

你需要实现一个机制,在master item过期时得到通知,以便你可以相应地维护相应的集合.

You need to implement a mechanism to be notified when the master item expires, so that you can maintain the corresponding sets accordingly.

查看这个问题的答案,我认为它适用于您的用例(用 ID 替换会话,用标签替换用户):

See the answer to this question, I think it applies to your use case (replace session by id, and user by tag):

Redis、会话过期和反向查找

这篇关于集合成员的 TTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆