Redis 集与散列 [英] Redis set vs hash

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

问题描述

在许多 Redis 教程中(例如这个),数据存储在一个集合中,但多个值组合在一起一起在一个字符串中(即用户帐户可能作为两个条目存储在集合中,user:1000:username"和user:1000:password").

In many Redis tutorials (such as this one), data is stored in a set, but with multiple values combined together in a string (i.e. a user account might be stored in the set as two entries, "user:1000:username" and "user:1000:password").

然而,Redis 也有哈希值.似乎有一个user:1000"哈希更有意义,它包含一个用户名"条目和一个密码"条目.无需连接字符串来访问特定值,您只需直接在哈希中访问它们即可.

However, Redis also has hashes. It seems that it would make more sense to have a "user:1000" hash, which contains a "username" entry and a "password" entry. Rather than concatenating strings to access a particular value, you just access them directly in the hash.

那为什么没有那么多使用呢?这些只是旧教程吗?还是 Redis 哈希有性能问题?

So why isn't it used as much? Are these just old tutorials? Or do Redis hashes have performance issues?

推荐答案

Redis 哈希非常适合存储更复杂的数据,就像您在问题中所建议的那样.我正是使用它们来存储具有多个需要缓存的属性的对象(特别是电子商务网站上特定产品的库存数据).当然,我可以使用连接的字符串 - 但这会给我的客户端代码增加不必要的复杂性,并且无法更新单个字段.

Redis hashes are good for storing more complex data, like you suggest in your question. I use them for exactly that - to store objects with multiple attributes that need to be cached (specifically, inventory data for a particular product on an e-commerce site). Sure, I could use a concatenated string - but that adds unneeded complexity to my client code, and updating an individual field is not possible.

您可能是对的 - 教程可能只是在引入哈希之前.它们显然是为存储对象表示而设计的:http://oldblog.antirez.com/post/redis-weekly-update-1.html

You may be right - the tutorials may simply be from before Hashes were introduced. They were clearly designed for storing Object representations: http://oldblog.antirez.com/post/redis-weekly-update-1.html

我想一个问题是,与简单的 String SET 命令相比,当插入新项目时 Redis 必须服务的命令数量(n 个命令,其中 n 是哈希中的字段数).对于每天访问 Redis 大约 100 万次的服务,我还没有发现这是一个问题.对我来说,使用正确的数据结构比微不足道的性能影响更重要.

I suppose one concern would be the number of commands Redis must service when a new item is inserted (n number of commands, where n is the number of fields in the Hash) when compared to a simple String SET command. I haven't found this to be a problem yet on a service which hits Redis about 1 million times per day. Using the right data structure to me is more important than a negligible performance impact.

(另外,请参阅我关于 Redis 集合与 Redis 字符串的评论 - 我认为您的问题是指字符串,但如果我错了,请纠正我!)

(Also, please see my comment regarding Redis Sets vs. Redis Strings - I think your question is referring to Strings but correct me if I'm wrong!)

这篇关于Redis 集与散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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