String
1, String Common command: In addition to get, set, INCR, DECR Mget and other operations, Redis also provides the following actions: Get the string length to the string append content settings and get a string of content settings and get a string of one (bit) Bulk set the contents of a series of strings 2, Application scenario: string is the most common type of data, ordinary Key/value storage can be classified as such, value is not only a string, can also be a number: for example, want to know when to block an IP address (access more than a few times). The Incrby command makes it easy to keep count by atomic increment. 3, Implementation mode: M,DECR and other operations will be converted to a numerical type for calculation, at this time Redisobject encoding field is int.
Hash
1, common command: hget,hset,hgetall and so on.   2, Application Scenario: we simply cite an example to describe the application scenario of the hash, for example, we want to store a user information object data, including the following information: User ID, key, for the lookup stored Value User object contains name name, age, birthday birthday and other information, if you use normal key /value structure to store, mainly has the following 2 kinds of storage methods: the first way to find the user ID as key, Encapsulates other information as an object to store, in a serialized manner such as: set u001 "Lie triple, 18,20010101" The disadvantage of this approach is that the overhead of serialization/deserialization is increased, 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 how many members of this user information object are saved into the Key-value, using the user id+ The name of the corresponding property is used as a unique identifier to obtain the value of the corresponding property, such as: Mset user:001:name "Lie triple " user:001:age18 user:001:birthday "20010101" Although there is no serialization overhead and concurrency problems, but the user ID is a duplicate storage, if there is a large number of such data, memory waste is very considerable. then the hash provided by Redis solves this problem very well, and the Redis hash is actually stored internally as value for a hashmap, provides direct access to this map member's interface, such as: Hmset user:001 name "Lie Triple" age 18 birthday "20010101" that is, key is still user Id,value is a map, The key of this map is the property name of the member, and value is the property value, The data can be modified and accessed directly through its internal map key (Redis called internal map key field), that is through the key (User ID) + field (property label) operation corresponding to the attribute data, There is no need to store data repeatedly, nor does it lead to problems with serialization and concurrency modification control. A good solution to the problem. It is also important to note that Redis provides an interface (Hgetall) that can fetch all the property data directly, but if the internal map has many members, This involves traversing the entire internal map, which may be time-consuming due to the Redis single-threaded model, and other clients ' requests are completely unresponsive, which requires extra attention. 3, the implementation of: above has been said Redis hash corresponding to the value of the actual is a hashmap, the actual here will have 2 different implementations, This hash of the members relatively young Redis in order to save memory will be similar to a one-dimensional array of compact storage, without the use of real hashmap structure, the corresponding Value redisobject encoding is ZIPMAP, When the number of members increases, it automatically turns into a true hashmap, at which point encoding is HT.
This article is from the "PHP Program ape" blog, please be sure to keep this source http://okowo.blog.51cto.com/4923464/1690733
Five types of Redis data type application Scenarios