Redis 键的命名约定和有效字符 [英] Naming Convention and Valid Characters for a Redis Key

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

问题描述

我想知道在 Redis 密钥中哪些字符被认为是有效的.我用谷歌搜索了一段时间,找不到任何有用的信息.

I was wondering what characters are considered valid in a Redis key. I have googled for some time and can not find any useful info.

就像在 Python 中一样,有效的变量名应该属于 [a-zA-Z0-9_] 类.Redis 密钥有哪些要求和约定?

Like in Python, valid variable name should belong to the class [a-zA-Z0-9_]. What are the requirements and conventions for Redis keys?

推荐答案

部分答案这里,但这并不完全是重复的,因为您在询问允许的字符和约定.

Part of this is answered here, but this isn't completely a duplicate, as you're asking about allowed characters as well as conventions.

至于Redis 键中的有效字符手册对此进行了完整的解释:

Redis 键是二进制安全的,这意味着您可以使用任何二进制序列作为键,来自像foo"这样的字符串.JPEG 文件的内容.空字符串也是一个有效的键.

Redis keys are binary safe, this means that you can use any binary sequence as a key, from a string like "foo" to the content of a JPEG file. The empty string is also a valid key.

关于密钥的其他一些规则:

A few other rules about keys:

很长的键不是一个好主意,例如 1024 字节的键是一个坏主意,不仅在内存方面,而且因为在数据集中查找键可能需要多次昂贵的键比较.即使手头的任务是匹配一个大值的存在,诉诸散列(例如使用 SHA1)也是一个更好的主意,尤其是从内存和带宽的角度来看.

Very long keys are not a good idea, for instance a key of 1024 bytes is a bad idea not only memory-wise, but also because the lookup of the key in the dataset may require several costly key-comparisons. Even when the task at hand is to match the existence of a large value, to resort to hashing it (for example with SHA1) is a better idea, especially from the point of view of memory and bandwidth.

非常短的键通常不是一个好主意.写u1000flw"没什么意义如果您可以改写user:1000:followers",则将其作为键.后者更具可读性,与键对象本身和值对象使用的空间相比,增加的空间较小.虽然短键显然会消耗更少的内存,但您的工作是找到正确的平衡点.

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.

尽量坚持使用模式.例如object-type:id"是个好主意,如user:1000".点或破折号通常用于多词字段,如comment:1234:reply.to".或comment:1234:reply-to".

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:1234:reply.to" or "comment:1234:reply-to".

允许的最大密钥大小为 512 MB.

The maximum allowed key size is 512 MB.

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

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