redis key 命名規(guī)范 鍵名稱中的冒號 : 命名空間層次的表示

數(shù)據(jù)庫

46 篇文章0 訂閱

訂閱專欄

結(jié)論

redis中key的命名,用:分隔不同的層次|命名空間,如:user:id12345:contact

如果某個對象有字段的字段,用.連接。如user:id12345:contact.mail。

以及一款redis的可視化操作工具:Redis Desktop Manager。

Links

redis官網(wǎng)文檔:https://redis.io/topics/data-types-intro

SO社區(qū)問答:https://stackoverflow.com/questions/6965451/redis-key-naming-conventions

國內(nèi)社區(qū)問答: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點:

其他分隔符

讀過文首三個鏈接會發(fā)現(xiàn)以下格式的ID:

user:id12345:contact 表示user表的ID為id12345的記錄的字段contact。(那這個key的值就是對應(yīng)的字段的值了)

user::id12345::contact或user:::id12345:::contact,即多層冒號分隔。

user/id12345/contact

使用多層冒號分隔、使用/分隔,我測試過后,都可以正常獲取key的值。但是:

在RedisDesktopManager這款Redis可視化管理工具中,只有使用單個:分隔的key名稱,層次看起來最舒服【圖不貼了,有興趣自己試試看】。

另外redis官網(wǎng)也是介紹的:,所以key名稱的層次分隔符就推薦單個冒號:

最后一個字段contact,如果聯(lián)系方式包含三種:tel, mail, qq,怎么命名?

官網(wǎng)說可以使用.或-連接,如:user:id12345:contact.mail或user.id12345.contact-mail表示用戶表中ID為id12345的記錄的contact屬性中的mail屬性值。

————————————————

版權(quán)聲明:本文為CSDN博主「錦天」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/wuyujin1997/article/details/106267676/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容