在Redis中实现参考集 [英] Implement reference sets in Redis

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

问题描述

我需要在Redis中构建一个结构,以对涉及3个实体的以下问题建模:用户,节点和套接字(所有字符串ID). 例如,用户U1可以通过插槽S1和S2连接到节点N1,但是他也可以通过插槽S3和S4连接到节点N2.

I need to build a structure in Redis to model the following problem that involes 3 entities: user, node and socket (all string ids). For example, the user U1 can connect to the node N1 with the sockets S1 and S2, but also he can connect to the node N2 with the sockets S3 and S4.

当他连接或断开连接时,我必须能够轻松地更新连接到特定用户节点的套接字. 此外,如果不采取任何措施,则每个套接字必须在一定时间后过期. 在任何给定时间,我需要知道用户是否与至少一个套接字(独立于节点)连接.

I must be able to easily update the sockets connected to a node of a specific user, as he connects or disconnects. Also, each socket must expire after a certain amount of time if no action is taken. At any given time I need to know if a user is connected with at least one socket (independently of the node).

我使用2种集合(对群集使用井号)对问题进行建模: 1-密钥为{u:U1} skt:NX的集合,其中包含例如用户U1连接到N1的套接字 2-具有密钥{u:U1} skts的集合,其中包含其他集合,例如"{u:U1} skt:N1","{u:U1} skt:N2",...

I modeled the problem using 2 kinds of set (I used hashtag for the cluster): 1- a set with key {u:U1}skt:NX, which contains sockets connected , for example, to N1 for the user U1 2- a set with key {u:U1}skts, that contains the other sets, for example, "{u:U1}skt:N1", "{u:U1}skt:N2", ...

通过这种结构,我可以通过添加或删除成员来轻松更新集{u:U1} skt:NX.同样,我可以使用以下lua脚本来检查用户是否与套接字连接(无论节点如何).

With this structure I can easily update the sets {u:U1}skt:NX, by adding or removing members. Also I can use the following lua script to check if a user is connected with a socket (regardless of the node).

local indexes = redis.call("smembers", KEYS[1])
return redis.call("sunion", unpack(indexes))

这是正确的方法吗?如何实现集合成员的过期时间?

Is this the right approach? How can I implement the expire time of a member of a set?

推荐答案

Redis不会终止数据结构的内容.在集合中实现到期的一种常见模式是改用排序集合,将时间戳或版本值存储在元素的分数中.然后,删除具有足够低分数的成员(使用 ZREMBYSCORE 命令)就很简单了.

Redis does not sport expiry of data structures' contents. A common pattern to implement expiry in Sets is to use Sorted Sets instead, storing a timestamp or version value in the elements' score. Expiry then becomes a simple matter of removing members having low enough scores (with the ZREMBYSCORE command).

这篇关于在Redis中实现参考集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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