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/

猜你喜欢

转载自blog.csdn.net/zhongguowangzhan/article/details/127930791