Redis public data type
The most commonly used data types of redis mainly include the following five types:
line
mix
catalogue
set up
Sorted set
Before describing these data types in detail, let's learn how to describe these different data types in redis memory management through a picture:
First, redis uses a redisobject object to represent all keys and values. The main information of Redisobject is shown in the above figure: type stands for one.
What data type is a value object? Coding is the storage mode of different data types in redis. For example, type=string represents value storage.
An ordinary string, then the corresponding encoding can be raw or int. If it is int, it means that the actual redis stores and represents this string according to the numerical type. of course,
The premise is that the string itself can be represented by a numerical value, such as "123".
A string like "456".
The vm field needs to be explained here. Only when the virtual memory function of redis is turned on will this field really allocate memory. This function is turned off by default, and this function will be introduced in detail later. get through
In the above figure, we can find that redis uses redisobject to represent all key/value data, which is a waste of memory. Of course, these memory management costs are mainly given to
Redis provides a unified management interface for different data types, and the actual author also provides various methods to help us save memory as much as possible, which will be discussed in detail later.
Let's analyze the purpose and internal implementation of these five data types one by one:
line
Common commands:
Set, acquire, reduce, increase, manage, etc.
Application scenario:
String is the most commonly used data type, and ordinary key/value storage can be classified into this category, so I will not explain it here.
Implementation mode:
The string stored in redis is a string by default and is referenced by redisobject. When encountering incr, decr and other operations, it will be converted into numerical type for calculation. At this point, the coding field of redisobject is int.
mix
Common commands:
Hget, hset, hgetall, etc.
Application scenario:
Let's give a simple example to describe the application scenario of hash. For example, we want to store a user information object data, which contains the following information:
User id is the keyword to be searched, and the stored value user object contains information such as name, age and birthday. If it is stored in a common key/value structure, there are two main storage methods:
The first method takes the user id as the search key, and encapsulates other information into an object and stores it in a serialized way. The disadvantage of this method is that it increases the cost of serialization/deserialization. When one piece of information needs to be modified, the whole object needs to be retrieved, and the modification operation needs to protect concurrency and introduce cas and other complex problems.
The second method is to store the user information object as many key-value pairs as there are members, and use the user id+ the name of the corresponding attribute as the unique identifier to obtain the value of the corresponding attribute. Although the serialization overhead and concurrency problems are eliminated, the user id is stored repeatedly, and if there is a large amount of such data, the memory waste is still very considerable.
Then the hash provided by redis solves this problem well. The hash of redis is actually that the internally stored value is a hashmap, which provides an interface for directly accessing this map member, as shown in the following figure:
That is, the key is still the user id,
Value is a mapping, and the key of this mapping is the attribute name of the member, and value is the attribute value, so that data can be directly modified and accessed through the key of its internal mapping (the key of internal mapping is called field in Redis).
That is, through key (user id)+field (attribute tag).
The corresponding attribute data can be manipulated, and there is no need to store data repeatedly, and it will not bring the problems of serialization and concurrent modification control. It solves this problem well.
At the same time, it should be noted that redis provides an interface (hgetall), which can directly obtain all the attribute data, but if there are many members of the internal mapping, it involves traversing the entire internal mapping.
Operation, because of the redis single-threaded model, this traversal operation may be time-consuming, while other clients' requests are completely unresponsive, which requires special attention.
Implementation mode:
It's been said about redis.
The value corresponding to hash is actually a hashmap. Actually, there are two different implementations. When the number of hash members is small, redis will use a way similar to one-dimensional array for compact storage, instead of using the real hashmap structure and corresponding values.
The code of redisobject is zipmap, which will automatically become a real hashmap when the number of members increases, and the code is ht.
catalogue
Common commands:
Lpush, rpush, lpop, rpop, lrange, etc.
Application scenario:
Radice
List has many application scenarios and is one of the most important data structures of redis. For example, twitter's follow-up list and fan list can be realized through the list structure of redis, which is easy to understand and will not be repeated here.
Implementation mode:
Radice
The implementation of list is a bidirectional linked list, which can support reverse search and traversal and is easier to operate, but it brings some extra memory overhead. Many implementations within redis, including send buffer queues, also use this data structure.
set up
Common commands:
Sadd, spop, sembers, sunion, etc.
Application scenario:
Radice
The function provided by set is similar to that of list, but the special point is that set can be copied automatically. When you need to store a list of data and don't want duplicate data, set is a good choice. set provides an important interface to judge whether members are in a set, which list can't provide.
Implementation mode:
The internal implementation of set is a.
Hashmap whose value is always null is actually sorted quickly by calculating hash, which is why set can provide a way to judge whether members are in a collection.
Sorted set
Common commands:
Zade, zrange, zrem, zcard, etc.
Usage scenario:
The usage scenario of redis sorting set is similar to that of set, except that SET does not sort automatically.
Set can sort members by providing an additional parameter score, and it is inserted in an orderly manner, that is, it is automatically sorted. When you need an ordered and non-repetitive favorite list, you can choose sorting.
Set data structures, such as twitter's public.