Redis 和查询值 [英] Redis and Querying Values

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

问题描述

Redis 在概念上与我使用的传统 SQL 数据库不同,我正试图弄清楚它是否适合我的项目......我一直在环顾四周,但似乎无法找到我的答案问题.

Redis is conceptually different from traditional SQL databases that I use, and I'm trying to figure out if it is right for my project... I've been looking around but can't seem to find an answer to my question.

我有一组需要存储的用户,每个用户都有一个唯一的 ID 和几个与之关联的值(例如他们的姓名).似乎我可以简单地将它们存储为哈希:

I have a set of Users that I need to store, each with a unique ID and several values (such as their name) associated with it. It seems like I can simply store those as a hash:

user:fef982dcfe1a7bcba4849b4c281bba95
"username" "andrewm" "name" "Andrew"

我还有一堆要存储的消息,每个消息都有一些属性,例如发件人和收件人:

I also have a bunch of messages I want to store, each having a few properties such as the sender and recipient:

message:1a7bcba4849b4c281bfef98a952dcfeb
"sender" "fef982dcfe1a7bcba4849b4c281bba95" "recipient" "82dcfe1a7bcba4849b4c281bba95fef9" "message" "Hi!"

我的问题是,我将如何检索特定用户发送的所有消息(由其哈希指定).我应该使用传统的关系数据库,还是像 MongoDB(我以前使用过)这样的 NoSQL 数据库?如果是这样,有人对高性能商店有什么建议吗?我不会做任何真正的搜索(即 MySQL LIKE 查询)——只是键值查找,真的.

My question is, how would I go about retrieving all of the messages that are sent by a specific user (designated by a their hash). Should I be using a traditional relational database instead, or even a NoSQL database like MongoDB (which I've used before)? If so, does anyone have any suggestions for high performance stores? I won't be doing any true searching (i.e. MySQL LIKE queries)-- just key value lookups, really.

推荐答案

当然可以使用 Redis 对这些数据进行建模,但是您需要考虑数据结构和访问路径.使用 Redis,访问路径不是隐式管理的(就像 RDBMS/MongoDB 中的索引一样).

It is certainly possible to model these data with Redis, but you need to think in term of data structures AND access paths. With Redis the access paths are not managed implicitly (like with indexes in RDBMS/MongoDB).

对于提供的示例,您可以:

For the provided example, you could have:

user:<user hash> -> hash of user properties
user:<user hash>:sent -> set of <msg hash>
user:<user hash>:received -> set of <msg hash>
message:<msg hash> -> hash of message properties

添加/删除消息意味着除了添加/删除消息对象本身之外,还要维护与发送者和接收者相对应的 *:sent 和 *:received 集.

Adding/deleting a message would mean maintaining the *:sent and *:received sets corresponding to the senders and recipients, on top of adding/deleting the message object itself.

为给定用户检索已发送或已接收的消息只是一个 SMEMBERS 命令,如果您还想同时检索消息的属性,则可以使用 SORT:

Retrieving sent or received messages for a given user is just a SMEMBERS command, or a SORT if you want to retrieve also the properties of the message at the same time:

# Get a list of message hash codes only in one roundtrip
smembers user:<user hash>:received

# Get a list of message contents in one roundtrip
sort user:<user hash>:received by nosort get message:*->sender get message:*->message

有关使用排序的基本原理,请参阅:

For the rationale about using sort, see:

注意 1:对于 Redis,最好使用整数作为键而不是 UUID 或哈希码(尤其是在集合中),因为它们以更有效的方式存储.

Note 1: with Redis it is better to use integers as keys rather than UUID or hash codes (especially in sets), since they are stored in a more efficient way.

注意 2:如果您需要对消息进行排序,则必须使用列表而不是集合.结果是只能删除最旧的消息,并且只能以有效的方式添加新消息.您可能还会为所有消息添加一个全局列表.

Note 2: if you need to order the messages, then lists must be used instead of sets. The consequence is only oldest messages can be removed, and only newset messages can be added in an efficient way. You would probably to also add a global list for all messages.

这篇关于Redis 和查询值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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