In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what is the use of the Hash type in redis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The Hash type is the field and value mapping table of the String type, or a String collection, which is particularly suitable for storing objects. By comparison, storing an object type in a Hash type takes up less memory space than a String type class, and is convenient to access the entire object.
In Redis, the hash type means that the key value itself is a key-value pair structure, such as: value= {{field1,value1}, {field2,value2}, {fieldN,valueN}}
Common commands:
Hget,hset,hgetall et al.
Application scenarios:
Let's give a simple example to describe the application scenario of Hash. For example, we want to store a user information object data that contains the following information:
User ID, which is the key found
The stored value user object contains information such as name name, age age, birthday birthday and so on.
If you use ordinary key/value structure to store, there are two main storage methods:
The first method uses the user ID as a lookup key, encapsulating other information into an object and storing it in a serialized way.
Such as: set U001 "Li San, 18pm 20010101"
The disadvantage of this approach is that it increases the cost of serialization / deserialization, and when one of the information needs to be modified, the entire object needs to be retrieved, and the modification operation needs to protect concurrency and introduce complex problems such as CAS.
The second method is to save as many key-value pairs as the user information object has, and use the name of the attribute corresponding to the user ID+ as the unique identification to obtain the value of the corresponding attribute, such as mset user:001:name "Li San" user:001:age18 user:001:birthday "20010101".
Although the serialization overhead and concurrency problems are eliminated, the user ID stores repeatedly, and if there is a large amount of such data, the memory waste is still considerable.
Then the Hash provided by Redis solves this problem very well. The Hash of Redis is actually an internally stored Value as a HashMap, and provides an interface to directly access this Map member.
Such as: hmset user:001 name "Li San" age 18 birthday "20010101"
In other words, the Key is still the user ID,value is a Map, the key of the Map is the attribute name of the member, and the value is the attribute value, so the data can be modified and accessed directly through the Key of the internal Map (the key of the internal Map is called field in the Redis), that is, through the key (user ID) + field (attribute tag) operation corresponding to the attribute data, there is no need to store the data repeatedly. Nor does it cause problems with serialization and concurrency modification control. Solved the problem very well. It should also be noted that Redis provides hgetall to fetch all attribute data directly, but if there are many members of the internal Map, then the operation of traversing the entire internal Map is involved. Because of the Redis single-thread model, this traversal operation may be time-consuming, while the requests of other clients are completely unresponsive, which requires special attention.
Implementation method: it has been mentioned above that the Redis Hash corresponds to the internal Value is actually a HashMap. In fact, there are two different implementations. When the members of this Hash are relatively small, the Redis will use a compact storage method similar to an one-dimensional array in order to save memory, but will not use the real HashMap structure. The encoding of the corresponding value redisObject is zipmap, and when the number of members increases, it will be automatically converted to the real HashMap, and the encoding will be ht.
About what the Hash type in redis is useful to share here, I hope the above content can be of some help to you, you can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.