What is the purpose of colons within Redis keys

后端 未结 2 479
名媛妹妹
名媛妹妹 2021-01-30 06:08

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 n

2条回答
  •  执念已碎
    2021-01-30 06:44

    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.

提交回复
热议问题