如何使用 Redis 列表来实现聊天系统? [英] How Redis lists may be used to implement a chat system?

查看:70
本文介绍了如何使用 Redis 列表来实现聊天系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 Redis 介绍的示例:

$ redis-cli rpush messages "Hello how are you?"
OK
$ redis-cli rpush messages "Fine thanks. I'm having fun with Redis"
OK
$ redis-cli rpush messages "I should look into this NOSQL thing ASAP"
OK
$ redis-cli lrange messages 0 2
1. Hello how are you?
2. Fine thanks. I'm having fun with Redis
3. I should look into this NOSQL thing ASAP

下面写着:

从上面的例子你可以猜到,列表可以按顺序使用实现聊天系统.

As you can guess from the example above, lists could be used in order to implement a chat system.

我的问题是:他们说实现聊天系统的真正含义是什么?

My question is: what do they really mean saying to implement a chat system?

例如,聊天中的一条消息至少有三个参数:

For example, a message in a chat has at least three parameters:

1) 一条消息文本,

2) 消息的作者,

3) 写消息的时间.

在上面的代码示例中,我只看到一个参数:消息文本.

In the code example above I see only one parameter: a text of a message.

那么如何使用列表来实现聊天系统呢?他们假设其他两个参数存储在哪里以及如何将它们连接到Redis列表中的消息?

So how lists may be used to implement a chat system? Where do they suppose to store other two parameters and how to connect them to a message in Redis list?

UPD:

我找到了一本很好的书来了解 Redis 是什么:

I found a great book to understand what Redis is:

http://openmymind.net/2012/1/23/The-Little-Redis-Book/

它简短、简单但信息量很大.

It is short, simple but very informative.

推荐答案

这里有两种可能:

  • 使用参考.您可以将消息放入包含多个字段的哈希对象中.该散列的键必须从序列 (incr) 生成,或者可以是 UUID 或任何其他唯一标识符.此键将是对您的消息的引用,因此您只需将其推送到列表中而不是消息文本中.从列表中弹出引用后,由接收者获取消息的内容.

  • Use a reference. You can put your message in a hash object containing several fields. The key of this hash must be generated from a sequence (incr), or can be a UUID, or any other unique identifier. This key will be the reference to your message, so you just have to push it in the list instead of the text of your message. Up to the receiver to fetch the content of the message once the reference has been popped from the list.

使用序列化.只需使用您想要的任何格式(例如:json、xml、messagepack 等)序列化消息的各个字段,然后将结果推送到列表中.显然,接收者必须反序列化该项目以检索各个字段.

Use serialization. Just serialize the various fields of your message using any format you want (example: json, xml, messagepack, etc ...), and push the result to the list. Obviously, the receiver will have to deserialize the item to retrieve the various fields.

这篇关于如何使用 Redis 列表来实现聊天系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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