gpt4 book ai didi

Redis在获取键值时响应各种$值

转载 作者:可可西里 更新时间:2023-11-01 11:14:58 26 4
gpt4 key购买 nike

我目前正在考虑使用 Redis 并为它制作一个客户端库。

我正在做一些实验,通过在端口 6379 上创建到 redis 服务器的 telnet 连接来了解它是如何工作的。

当我使用 SET mykey myvalue 设置一个键时,它返回 +OK。 + 是否表示任何内容,因为它似乎没有在任何地方记录。

然后当我尝试使用 GET mykey 获取 key 时,我得到以下信息

$7
myvalue

根据我在文档中看到的内容,它只支持返回值,所以为什么我要退回 7 美元。它有什么特别的意义吗,或者我可以把它废弃并只看下一行。

最佳答案

这在 Redis Protocol specification 中有详细记录.我建议您在尝试编写自己的客户端和/或现有的 Redis clients 之一之前通读它。

+ 表示一个简单的字符串 - 引用:https://redis.io/topics/protocol#resp-simple-strings

Simple Strings are encoded in the following way: a plus character, followed by a string that cannot contain a CR or LF character (no newlines are allowed), terminated by CRLF (that is "\r\n").

Simple Strings are used to transmit non binary safe strings with minimal overhead. For example many Redis commands reply with just "OK" on success, that as a RESP Simple String is encoded with the following 5 bytes:

"+OK\r\n"

$ 表示批量字符串 - 引用:https://redis.io/topics/protocol#resp-bulk-strings

Bulk Strings are used in order to represent a single binary safe string up to 512 MB in length.

Bulk Strings are encoded in the following way:

  • A "$" byte followed by the number of bytes composing the string (a prefixed length), terminated by CRLF.
  • The actual string data.
  • A final CRLF.

So the string "foobar" is encoded as follows:

"$6\r\nfoobar\r\n"

关于Redis在获取键值时响应各种$值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com