问题描述
我正在学习如何将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.
我看到过这样的键名:
users:bob
color:blue
item:bag
冒号是否将密钥分为几类,从而使查找密钥的速度更快?如果是这样,您在命名键时可以使用多个冒号将它们划分为子类别吗?最后,它们与在Redis服务器中定义不同的数据库有什么关系吗?
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?
我已经阅读了文档,并对此事进行了大量Google搜索,但奇怪的是我找不到任何讨论此事的东西.
I have read through documentation and done numerous Google searches on the matter but oddly I can't find anything discussing this.
推荐答案
冒号已在早期的redis版本中用作存储命名空间数据的概念.在早期版本中,redis仅支持字符串,如果您想存储电子邮件和'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 a string, so colons were used:
SET user:bob:email [email protected]
SET user:bob:age 31
它们在Redis中没有没有特殊的处理或性能特征,唯一的目的是对数据进行命名间隔以再次找到它.如今,您可以使用散列来存储大多数带空格的键:
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
您不必将哈希命名为"user:bob",我们可以将其命名为"bob",但是使用用户前缀命名它的间隔,我们会立即知道此哈希应该/应该包含哪些信息.
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.
这篇关于Redis键中冒号的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!