String
Common commands:
Set,get,decr,incr,mget and so on.
Application Scenarios:
String is the most commonly used type of data, and ordinary key/value storage can be categorized as such, which is not explained here.
Implementation method:
String in the Redis internal storage By default is a string, referenced by Redisobject , when encountered INCR,DECR and other operations will be converted to a numeric type for calculation, at this time Redisobject encoding field is an int.
Hash
Common commands:
Hget,hset,hgetall and so on.
Application Scenarios:
Let's simply cite an example to describe the application scenario for a hash, such as storing a user information object data that contains the following information:
The user ID is the key to find, the stored value user object contains the name, age, birthday and other information, if the ordinary key/value structure to store, mainly has the following 2 kinds of storage methods:
The disadvantage of using the user ID as a lookup key and encapsulating other information as an object to be stored in a serialized way is that the cost of serialization/deserialization is increased , and when one of the information needs to be modified, the whole object needs to be retrieved. And the modification operation needs to protect the concurrency and introduce complex problems such as CAs.
The second method is how many members of this user information object will be saved into the number of key-value, with the user id+ the name of the corresponding property as a unique identifier to obtain the value of the corresponding property, although the cost of serialization and concurrency is omitted, but the user ID is repeated storage, if there is a large number of such data, The memory waste is still very considerable.
So the hash provided by Redis is a good solution to this problem, and the Redis hash is actually the internal stored value as a hashmap, and provides a direct access to the map member's interface, such as:
That is,key is still the user ID, value is a map, the map key is a member of the property name , value is the property value, so that the data modification and access can be directly through its internal map key ( Redis called the internal Map key field), that is, through key (user ID) + field (attribute tag) can manipulate the corresponding attribute data, do not need to store data repeatedly, and do not bring serialization and concurrency modification control problems. A good solution to the problem.
It is also important to note that Redis provides an interface (Hgetall) that can fetch all of the property data directly, but if the internal map has a lot of members, it involves traversing the entire internal map, because of the Redis single-threaded model, This traversal operation can be time-consuming, and other client requests are completely unresponsive, which requires extra attention.
Implementation method:
The above has been said that the Redis hash corresponds to value inside the actual is a hashmap, actually there will be 2 different implementations, this hash of the members of the relatively small redis in order to save memory will be similar to a one-dimensional array to compact storage , instead of a true hashmap structure, the corresponding value Redisobject encoding is Zipmap, and when the number of members increases, it automatically turns into true HashMap, at which time encoding is HT.
List
Common commands:
Lpush,rpush,lpop,rpop,lrange and so on.
Application Scenarios:
Redis list has many applications and is one of the most important data structures of redis, such as Twitter's watchlist, fan list, etc., which can be implemented using the REDIS list structure, which is better understood and not repeated here.
Implementation method:
the implementation of Redis list is a doubly linked list , which can support reverse lookup and traversal, but it is more convenient to operate, but it brings some additional memory overhead , and many implementations within Redis, including sending buffer queues, are also used in this data structure.
Set
Common commands:
Sadd,spop,smembers,sunion and so on.
Application Scenarios:
The functionality provided by Redis set externally is a list-like feature, except that set is automatically weight- saving, and set is a good choice when you need to store a list of data and you don't want duplicate data . and set provides an important interface to determine whether a member is within a set set, which is not available in list.
Implementation method:
The internal implementation of set is a value that is always null hashmap, which is actually calculated by hashing the way to fast weight, which is also set to provide a judge whether a member is within the cause of the collection.