redis key 命名規範 鍵名稱中的冒號 : 命名空間層次的表示


數據庫
46 篇文章0 訂閱
訂閱專欄
結論
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".
1
2
即下圖中的第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屬性值。
————————————————
版權聲明:本文爲CSDN博主「錦天」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/wuyujin1997/article/details/106267676/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章