Redis 集 VS hash

在许多 Redis 教程(比如 这个)中,数据存储在一个集合中,但是多个值组合在一个字符串中(例如,一个用户帐户可以作为两个条目存储在集合中,“ user: 1000: username”和“ user: 1000: password”)。

然而,Redis 也有散列。似乎使用“ user: 1000”散列更有意义,它包含一个“ username”条目和一个“ password”条目。不需要连接字符串来访问特定的值,只需直接在散列中访问它们。

那么为什么它没有被广泛使用呢? 这些只是旧的教程吗? 还是 Redis 散列有性能问题?

69816 次浏览

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.

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

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.

(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!)

Hashes are one of the most efficient methods to store data in Redis, even going so far as to recommending them for use whenever effectively possible.

http://redis.io/topics/memory-optimization

Use hashes when possible

Small hashes are encoded in a very small space, so you should try representing your data using hashes every time it is possible. For instance if you have objects representing users in a web application, instead of using different keys for name, surname, email, password, use a single hash with all the required fields.

Use case comparison:

Sets provide with a semantic interface to store data as a set in Redis server. The use cases for this kind of data would be more for an analytics purpose, for example how many people browse the product page and how many end up purchasing the product.

Hashes provide a semantic interface to store simple and complex data objects in the Redis server. For example, user profile, product catalog, and so on.

Ref: Learning Redis

  • Use cases for SETS

    • Uniqueness:

      We have to enforce our application to make sure every username can be used by one single person. If someone signup with a username, we first look up set of usernames

      SISMEMBER setOfUsernames newUsername
      
    • Creating relationships between different records:

      Imagine you have Like functionality in your app. you might have a separate set for every single user and store the ID's of the images that user has liked so far.

    • Find common attributes that people like

      In dating apps, users usually pick different attributes, and those attributes are stored in sets. And to help people match easily, our app might check the intersection of those common attributes

      SINTER user#45:likesSet user#34:likesSet
      
    • When we have lists of items and order does not matter

      For example, if you want to restrict API addresses that want to reach your app or block emails to send you emails, you can store them in a set.

  • Use cases for Hash

Redis Hashes are usually used to store complex data objects: sessions, users etc. Hashes are more memory-optimized.