Redis键命名约定? [英] Redis key naming conventions?

查看:101
本文介绍了Redis键命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

redis中键的正常命名约定是什么?我已经看到用:隔开的值,但是我不确定正常的约定是什么,或者为什么.

What are the normal naming convention for keys in redis? I've seen values separated by : but I'm not sure what the normal convention is, or why.

对于用户,您会做类似...

For a user would you do something like...

user:00

如果用户的ID为00

您能否仅查询键的开头以返回所有用户?

Are you able to query for just the beginning of the key to return all users?

我主要是希望通过研究为人们工作的方式以及他们为什么选择他们的方式来避免将来出现任何问题.

I'm mainly just hoping to avoid any future problems by researching the ways that work for people and why they chose them.

推荐答案

redis中键的正常命名约定是什么?我见过 值以:分隔,但我不确定通常的约定是什么, 还是为什么.

What are the normal naming convention for keys in redis? I've seen values separated by : but I'm not sure what the normal convention is, or why.

是的,在命名键时,冒号:是一个约定.在Redis网站上的教程中指出:尝试坚持使用模式.例如,"object-type:id:field"可以是 一个好主意,例如在"user:1000:password"中.我喜欢用点 多词字段,例如在"comment:1234:reply.to"中.

Yes, colon sign : is a convention when naming keys. In this tutorial on redis website is stated: Try to stick with a schema. For instance "object-type:id:field" can be a nice idea, like in "user:1000:password". I like to use dots for multi-words fields, like in "comment:1234:reply.to".

您是否可以仅查询键的开头以返回全部 用户?

Are you able to query for just the beginning of the key to return all users?

如果您想直接查询以user:开头的所有键之类的东西,则有一个命令.但是,此命令应仅用于调试目的,因为它是 O(N),因为它正在搜索所有内容.密钥存储在数据库中.

If you mean someting like directly querying for all keys which starts with user: there is a keys command for that. This command should be however used only for debugging purpose since it's O(N) because it's searching through all keys strored in database.

针对此问题的更合适的解决方案是创建专用密钥,我们将其命名为users,它将存储所有用户密钥,例如,存储在列表 set 数据结构.

More appropriate solution for this problem is to create dedicated key, let's name is users, which will store all the users keys, for example, in list or set data structure.

这篇关于Redis键命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆