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

查看:32
本文介绍了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

if the user's id was 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"可一个不错的主意,就像在用户:1000:密码"中一样.我喜欢用点来表示多词字段,如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: 开头的所有键,则有一个 keys 命令.但是,此命令应仅用于调试目的,因为它是 O(N) 因为它正在搜索所有密钥存储在数据库中.

If you mean something 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 stored in database.

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

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

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

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