Five data types in Redis

Five data types in Redis

 

String

 

Common commands:

 

set,get,decr,incr,mget 等。

 

Application scenarios:

 

String is the most commonly used data type, and ordinary key/value storage can be classified into this category, which will not be explained here.

 

Method to realize:

 

String stored in redis is a string by default, which is referenced by redisObject. When incr, decr and other operations are encountered, it will be converted into a numeric type for calculation. At this time, the encoding field of redisObject is int.

 

 

Hash

 

Common commands:

 

hget,hset,hgetall 等。

 

Application scenarios:

 

Let's take a simple example to describe the application scenario of Hash. For example, we want to store a user information object data, including the following information:

The user ID is the search key, and the stored value user object contains information such as name, age, and birthday.

 

Three storage methods:

 

The first

The method uses the user ID as the search key, and encapsulates other information into an object and stores it in a serialized manner. The disadvantage of this method is that it increases the overhead 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 issues such as CAS.



 

the second

The method is to store as many key-value pairs as the user information object has, and use the user ID + the name of the corresponding attribute as the unique identifier to obtain the value of the corresponding attribute. Although serialization overhead and concurrency problems are eliminated, the user The ID is stored repeatedly. If there is a large amount of such data, the memory waste is still very considerable.



 Method three

The Hash of Redis is actually a HashMap internally stored Value, and provides an interface to directly access the members of this Map.

The key is still the user ID, the value is a Map, the key of this Map is the attribute name of the member, and the value is the attribute value, so that the modification and access to the data can be directly through the key of its internal map (redis called the internal map of the internal map) The key is field), that is, through the key (user ID) + field (attribute label), the corresponding attribute data can be manipulated, which does not require repeated data storage, nor does it bring serialization and concurrent modification control problems. Great solution to the problem.



 

Method to realize:

 

As mentioned above, Redis Hash corresponding to Value is actually a HashMap. In fact, there will be two different implementations. When there are few members of this Hash, Redis will use a one-dimensional array-like method for compact storage in order to save memory, instead of using real HashMap structure, the encoding of the corresponding value redisObject is zipmap, when the number of members increases, it will be automatically converted into a real HashMap, at this time the encoding is ht.

 

 

List

 

Common commands:

 

lpush,rpush,lpop,rpop,lrange, etc.

 

Application scenarios:

 

There are many application scenarios of Redis list, and it is also one of the most important data structures of Redis. For example, twitter follow list, fan list, etc. can be implemented with Redis list structure, which is easy to understand and will not be repeated here.

 

Method to realize:

 

The implementation of Redis list is a doubly linked list, which can support reverse search and traversal, which is more convenient to operate, but brings some additional memory overhead. Many internal implementations of Redis, including sending buffer queues, also use this data structure.

 

 

Set

 

Common commands:

 

sadd,spop,smembers,sunion 等。

 

Application scenarios:

 

The function provided by Redis set to the outside world is similar to that of list, which is a list function. The special feature is that set can automatically arrange weights. When you need to store a list of data and do not want duplicate data, set is a good choice. , and set provides an important interface for judging whether a member is in a set collection, which is also not provided by list.

 

Method to realize:

 

The internal implementation of set is a HashMap whose value is always null. In fact, it is used to quickly arrange weights by calculating the hash. This is why set can provide judgment whether a member is in the set.

 

 

Sorted set

 

Common commands:

 

zadd,zrange,zrem,zcard等

 

scenes to be used:

 

The usage scenario of Redis sorted set is similar to that of set, the difference is that set is not automatically sorted, while sorted set can sort members by providing an additional priority (score) parameter by the user, and it is inserted in order, that is, automatic sorting . When you need an ordered and non-repeating set list, you can choose the sorted set data structure. For example, the public timeline of twitter can be stored with the publication time as the score, so that it is automatically sorted by time when it is obtained.

 

Method to realize:

 

The interior of Redis sorted set uses HashMap and SkipList to ensure the storage and ordering of data. HashMap stores the mapping from members to scores, and the skip list stores all members. The sorting is based on the storage in HashMap. The score, using the structure of the jump table can obtain relatively high search efficiency, and is relatively simple in implementation.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487737&siteId=291194637