Redis 键中冒号的用途是什么

发布于 2024-09-15 09:09:10 字数 267 浏览 8 评论 0原文

我正在学习如何在我的项目中使用 Redis。我不明白的一件事是键名称中冒号的确切用途。

我见过这样的键名称:

users:bob
color:blue
item:bag

冒号是否将键分成类别并使查找键更快?如果是这样,您可以在命名键时使用多个冒号将它们分解为子类别吗?最后,它们与在 Redis 服务器中定义不同的数据库有什么关系吗?

我已经阅读了文档并就此事进行了大量谷歌搜索,但奇怪的是我找不到任何讨论此问题的内容。

I'm learning how to use Redis for a project of mine. One thing I haven't got my head around is what exactly the colons are used for in the names of keys.

I have seen names of key such as these:

users:bob
color:blue
item:bag

Does the colon separate keys into categories and make finding the keys faster? If so can you use multiple colons when naming keys to break them down into sub categories? Lastly do they have anything to do with defining different databases within the Redis server?

I have read through documentation and done numerous Google searches on the matter but oddly I can't find anything discussing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

上课铃就是安魂曲 2024-09-22 09:09:10

冒号作为存储命名空间数据的概念出现在早期的 Redis 版本中。在早期版本中,Redis 仅支持字符串。如果您想存储电子邮件和“bob”的年龄,则必须将其全部存储为字符串,因此使用冒号:

SET user:bob:email [email protected]
SET user:bob:age 31

它们在 Redis 中没有特殊处理或性能特征。唯一的目的是为数据命名空间以便再次找到它。如今,您可以使用哈希来存储大多数冒号键:

 HSET user:bob email [email protected]
 HSET user:bob age 31

您不必将哈希命名为“user:bob”,我们可以将其命名为“bob”,但是使用用户前缀命名它,我们立即知道该信息是哪些信息哈希应该/可以有。

The colons have been in earlier Redis versions as a concept for storing namespaced data. In early versions, Redis supported only strings. If you wanted to store the email and the age of 'bob' you had to store it all as strings, so colons were used:

SET user:bob:email [email protected]
SET user:bob:age 31

They had no special handling or performance characteristics in Redis. The only purpose was namespacing the data to find it again. Nowadays, you can use hashes to store most of the coloned keys:

 HSET user:bob email [email protected]
 HSET user:bob age 31

You don't have to name the hash "user:bob" we could name it "bob", but namespacing it with the user-prefix we instantly know which information this hash should/could have.

你怎么这么可爱啊 2024-09-22 09:09:10

冒号是构建键的一种方式。 Redis 不会以任何方式解释它们。您还可以使用您喜欢的任何其他分隔符或根本不使用。我个人更喜欢 /,它使我的密钥看起来像文件系统路径。它们对性能没有影响,但不应将它们设置得太长,因为 Redis 必须将所有键保留在内存中。

良好的键结构对于利用排序命令的强大功能非常重要,排序命令是 Redis 对 SQL 连接的回答。

GET user:bob:color   -> 'blue'
GET user:alice:color -> 'red'

SMEMBERS user:peter:friends -> alice, bob

SORT user:peter:friends BY NOSORT GET user:*:color   -> 'blue', 'red'

您可以看到键结构使 SORT 能够通过引用结构化键来查找用户的颜色。

Colons are a way to structure the keys. They are not interpreted by redis in any way. You can also use any other delimiter you like or none at all. I personally prefer /, which makes my keys look like file system paths. They have no influence on performance but you should not make them excessively long since redis has to keep all keys in memory.

A good key structure is important to leverage the power of the sort command, which is redis' answers to SQL's join.

GET user:bob:color   -> 'blue'
GET user:alice:color -> 'red'

SMEMBERS user:peter:friends -> alice, bob

SORT user:peter:friends BY NOSORT GET user:*:color   -> 'blue', 'red'

You can see that the key structure enables SORT to lookup the user's colors by referencing the structured keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文