redis key 键名称中的冒号 : 命名空间层次的表示

结论

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::contactuser:::id12345:::contact,即多层冒号分隔。
user/id12345/contact
使用多层冒号分隔、使用/分隔,我测试过后,都可以正常获取key的值。但是:
RedisDesktopManager这款Redis可视化管理工具中,只有使用单个:分隔的key名称,层次看起来最舒服【图不贴了,有兴趣自己试试看】。
另外redis官网也是介绍的:,所以key名称的层次分隔符就推荐单个冒号:

最后一个字段contact,如果联系方式包含三种:tel, mail, qq,怎么命名?
官网说可以使用.-连接,如:user:id12345:contact.mailuser.id12345.contact-mail表示用户表中ID为id12345的记录的contact属性中的mail属性值

  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值