結論
redis中key的命名,用:
分隔不同的層次|命名空間,如:user:id12345:contact
如果某個對象有字段的字段
,用.
連接。如user:id12345:contact.mail
。
以及一款redis的可視化操作工具:Redis Desktop Manager。
Links
redis官網文檔:https://redis.io/topics/data-types-intro
SO社區問答:https://stackoverflow.com/questions/6965451/redis-key-naming-conventions
國內社區問答:https://answer-id.com/51740587
原文:
Very short keys are often not a good idea. There is little point in writing "u1000flw" as a key if you can instead write "user:1000:followers". The latter is more readable and the added space is minor compared to the space used by the key object itself and the value object. While short keys will obviously consume a bit less memory, your job is to find the right balance.
Try to stick with a schema. For instance "object-type:id" is a good idea, as in "user:1000". Dots or dashes are often used for multi-word fields, as in "comment:qq12345:reply.to" or "comment:qq12345:reply-to".
即下圖中的第2、3
點:
其他分隔符
讀過文首三個鏈接會發現以下格式的ID:
user:id12345:contact
表示user
表的ID爲id12345
的記錄的字段contact
。(那這個key的值就是對應的字段的值了)
user::id12345::contact
或user:::id12345:::contact
,即多層冒號分隔。
user/id12345/contact
使用多層冒號分隔、使用/
分隔,我測試過後,都可以正常獲取key的值。但是:
在RedisDesktopManager
這款Redis可視化管理工具中,只有使用單個:分隔
的key名稱,層次看起來最舒服【圖不貼了,有興趣自己試試看】。
另外redis官網也是介紹的:
,所以key名稱的層次分隔符就推薦單個冒號:
最後一個字段contact
,如果聯繫方式包含三種:tel, mail, qq
,怎麼命名?
官網說可以使用.
或-
連接,如:user:id12345:contact.mail
或user.id12345.contact-mail
表示用戶表中ID爲id12345的記錄的contact屬性中的mail屬性值。