当然可以使用Redis对这些数据进行建模,但是您需要考虑数据结构和访问路径。使用Redis时,访问路径不会隐式管理(就像RDBMS /
MongoDB中的索引一样)。
对于提供的示例,您可以具有:
user:<user hash> -> hash of user propertiesuser:<user hash>:sent -> set of <msg hash>user:<user hash>:received -> set of <msg hash>message:<msg hash> -> hash of message properties
添加/删除消息意味着在添加/删除消息对象本身的基础上,维护与发送者和接收者相对应的:sent和:received集。
检索给定用户的已发送或已接收的消息只是SMEMBERS命令,如果要同时检索消息的属性,则也可以是SORT:
# Get a list of message hash pres only in one roundtripsmembers user:<user hash>:received# Get a list of message contents in one roundtripsort user:<user hash>:received by nosort get message:*->sender get message:*->message
注意1: 使用Redis时,最好将整数用作键而不是UUID或哈希码(尤其是在集合中),因为它们以更有效的方式存储。
注意2:
如果您需要订购消息,则必须使用列表而不是列表。结果是只能删除最旧的消息,并且只能以有效的方式添加新的消息。您可能还会为所有消息添加一个全局列表。



