What is the purpose of colons in Redis keys

I am learning how to use Redis for my project. I don’t have a head, what exactly are the colons used in key names.

I have seen key names such as:

users:bob color:blue item:bag 

Does the colon categorize individual keys and speed up key searches? If so, you can use multiple colons when to name keys to break them into subcategories? Finally, do they have anything to do with defining different databases on a Redis server?

I read the documentation and completed numerous Google searches on this, but, oddly enough, I can't find anything about it.

+53
redis key-value-store colon
Aug 24 '10 at 8:59
source share
2 answers

Colons were in previous versions of redis as a concept for storing data with names. In earlier versions, redis only supported strings, if you wanted to keep email and the age of "bob", you had to save all this as a string, so colons were used:

 SET user:bob:email bob@example.com SET user:bob:age 31 

They had special processing or no performance characteristics, but the only goal was to convert the data names to find them again. Currently, you can use hashes to store most of the colonized keys:

  HSET user:bob email bob@example.com HSET user:bob age 31 

You do not need to call the hash "user: bob", we could call it "bob", but namespacing it with a user prefix, we immediately find out what information this hash should have.

+70
Aug 24 '10 at 11:06
source share

Columns are a way to structure keys. They are not interpreted by redis in any way. You can also use any other delimiter you like, or nothing at all. I personally prefer / , which makes my keys look like a file system path. They do not affect performance, but you should not do them for too long, since redis must store all the keys in memory.

A good key structure is important for using the power of the sort command, which returns responses to an SQL connection.

 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 allows SORT to search for user colors by referring to structured keys.

+29
Aug 25 2018-10-10T00:
source share



All Articles