RedisUse of five data types (from: http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml)
1. String
Common commands:
Set, get, decr, incr, mget, etc.
Application scenarios:
String is the most common data type. Common Key/value storage can be classified as this type, so we will not explain it here.
Implementation Method:
String is a string by default stored in redis and is referenced by redisobject. When an incr, decr, or other operation is performed, it is converted to a numeric type for calculation. In this case, the encoding field of redisobject is int.
2. Hash
Common commands:
Hget, hset, hgetall, etc.
Application scenarios:
Let's take an example to describe the hash Application Scenario. For example, we want to store a user information object data that contains the following information:
User ID, which is the search key,
The stored value user object contains information such as name, age, and birthday,
If the common key/value structure is used for storage, there are two main storage methods:
The first method is to use the user ID as the search key, and encapsulate other information into an object for storage in serialized mode,
For example:SetU001 "San Li"
The disadvantage of this method is that it increases the serialization/deserialization overhead, and the entire object needs to be retrieved when one of the information needs to be modified, and the modification operation needs to protect concurrency, introduce CAS and other complex problems.
The second method is to store the key-value pair as many members of the user information object, and use the user ID + name of the corresponding attribute as a unique identifier to obtain the value of the corresponding attribute,
For example:MsetUser: 001: Name "Li San" User: 001: age18user: 001: Birthday "20010101"
Although serialization overhead and concurrency problems are saved, the user ID is retained. If there is a large amount of such data, the memory waste is still considerable.
The hash provided by redis can solve this problem very well. redis's hash is actually a hashmap of internally stored values and provides an interface to directly access this map member,
For example:HmsetUser: 001 name "Li San" age 18 birthday "20010101"
That is to say, the key is still the user ID, the value is a map, the key of the map is the property name of the member, and the value is the property value, in this way, you can directly modify and access data through the internal map key (the internal map key in redis is called field), that is, through the key (User ID) + field (attribute tag) can operate the corresponding attribute data, neither storing data repeatedly nor causing serialization and concurrency modification control problems. Solved the problem.
At the same time, it should be noted that redis provides an interface (hgetall) to directly retrieve all attribute data. However, if there are many internal map members, it involves traversing the entire internal map operation, because of the redis single-threaded model, this traversal operation may be time-consuming, and requests from other clients do not respond at all. This requires special attention.
Implementation Method:
As mentioned above, the value corresponding to redis hash is actually a hashmap. There are two different implementations here, when there are few Members of this hash, redis will adopt a compact storage like a one-dimensional array to save memory, instead of a real hashmap structure. The corresponding value redisobject's encoding is zipmap, when the number of members increases, it is automatically converted to a real hashmap. In this case, encoding is ht.
3. List
Common commands:
Lpush, rpush, lpop, rpop, and lrange.
Application scenarios:
Redis list has many application scenarios and is also one of redis's most important data structures. For example, the Twitter follow list and fans list can all be implemented using the redis list structure, which is easy to understand, this is not repeated here.
Implementation Method:
The implementation of redis list is a two-way linked list, that is, it supports reverse lookup and traversal to facilitate operations. However, it brings some additional memory overhead and many internal implementations of redis, this data structure is also used, including the sending Buffer Queue.
4. Set
Common commands:
Sadd, spop, smembers, and sunion.
Application scenarios:
The functions provided by redis set are similar to those provided by list. The special feature is that set can automatically record duplicates. When you need to store a list of data, if you do not want duplicate data, set is a good choice, and set provides an important interface to determine whether a member is in a set. This is also not provided by list.
Implementation Method:
The internal implementation of set is a hashmap whose value is always null. Actually, it is to calculate the hash method to quickly remove duplicates, this is also why set can determine whether a member is in the set.
5. Sorted set
Common commands:
Zadd, zrange, zrem, zcard, etc.
Use Cases:
The usage scenario of redis sorted set is similar to set. The difference is that set is not automatically ordered, while sorted set can sort members by providing an additional priority (score) parameter, it is insert-ordered, that is, automatic sorting. When you need an ordered and non-repeated list of sets, you can choose sorted set data structure. For example, Twitter's public timeline can be stored as score by posting time, in this way, the query is automatically sorted by time.
Implementation Method:
In redis sorted set, hashmap and skiplist are used internally to ensure data storage and order. In hashmap, Members are mapped to scores, the hop table stores all members, and the sorting is based on the score saved in hashmap. using the structure of the hop table, you can get a high search efficiency, and the implementation is relatively simple.