Redis Data TypesExperiment Introduction
Redis is not just a simple key-value memory, but also a data structures server. Traditional key-value refers to the use of a key string to index the storage of the value string, while in Redis, value not only supports strings, but also supports more complex structures, including lists, collections, hash tables, and so on. Now let's talk about this: Redis keys is binary safe, which means you can use any binary sequence as a focus, from a JPEG file like "foo". An empty string is also a valid key.
first, Redis strings
A string is the most basic type of Redis value. Redis strings are binary safe, which means that a Redis string can contain any type of data, such as a JPEG-formatted picture or a serialized Ruby object. A value of a string type can store up to 512M bytes of content.
Let's go on to the previous experiment and start REDIS-CLI to see the Redis strings data type.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/14/wKiom1WwwkiQZzorAAE7SJc3Blw961.jpg "title=" 1.png " alt= "Wkiom1wwwkiqzzoraae7sjc3blw961.jpg"/>650) this.width=650; "width=" + "height=" src= "/e/u261/themes/" Default/images/spacer.gif "style=" Background:url ("/e/u261/themes/default/images/word.gif") no-repeat Center; border:1px solid #ddd; "alt=" Spacer.gif "/>
As shown in the example above, you can create and retrieve strings by the set and get commands. Note that the SET command will replace any existing key that already exists. The SET command also has an option to provide additional parameters, and we can let the set command succeed without the same key, and vice versa, allowing the set command to succeed if the same key is worth the case.
Even though string is the basic type of redis, you can do some interesting things about it, such as the adder:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/14/wKiom1Wwwn2CoYqhAAIspjhewS0417.jpg "title=" 2.png " alt= "Wkiom1wwwn2coyqhaaispjhews0417.jpg"/>
The INCR command lets the value become an integer, running incr +1 at a time. The Incrby command is an addition operation. Similar commands such as subtraction are: DECR and Decrby.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/11/wKioL1WwxG7jlQaJAAHonfEHgEs700.jpg "title=" 3.png " alt= "Wkiol1wwxg7jlqajaahonfehges700.jpg"/>
Redis can use the Mset and MGET command to complete a one-time correspondence between multiple key-value, and with the MGET command, Redis returns an array of value.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/14/wKiom1WwwpXChGhpAAEEl48aqyk120.jpg "title=" 4.png " alt= "Wkiom1wwwpxchghpaaeel48aqyk120.jpg"/>
Second, Redis Lists
The Redis list is a simple list of strings, sorted by insertion order. You can add an element to the head of the list (left) or the tail (right) Lpush command to insert a new element to the head, and Rpush to insert a new element in the lead. When one of these two operations is executed on an empty key, a new list is created. Similarly, if a list operation clears a list then the corresponding key will be removed from the key space. This is very handy for semantics because they are called using an empty list exactly as they were called when using a nonexistent key value (CAN) as a parameter.
The return value of a command of type push is the length of the list. Some examples of class table operations and results:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/11/wKioL1WwxIWBI-SbAAHeKx0z3l8675.jpg "title=" 5.png " alt= "Wkiol1wwxiwbi-sbaahekx0z3l8675.jpg"/>
Note : Lrange takes advantage of two retrieval values, 0 means the first of the list starts, and 1 indicates the last of the list's penultimate, which is the final one. 2 is the penultimate second of the list, and so on.
These commands are variable commands, which means you can add multiple elements to a list at a time.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/14/wKiom1WwwqqQfw2TAAFlq-ivTNA988.jpg "title=" 6.png " Width= "height=" 254 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:254px; "alt=" Wkiom1wwwqqqfw2taaflq-ivtna988.jpg "/>
In the Redis command operation, there is also a class of important actions: POP, take out the list element. Like the push operation, the pop command can pick the element in a different direction. The pop command returns the element that is taken out.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/11/wKioL1WwxJmxWDGxAANnM2S2NhE360.jpg "title=" 7.png " Width= "415" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:415px;height:500px; "alt=" Wkiol1wwxjmxwdgxaannm2s2nhe360.jpg "/>
There are a number of useful commands in the Redis list, which are limited to a few pages.
third, Redis hashes
Redis hashes is a mapping between string fields and string values, so they are the perfect data type for presenting objects. (e.g. a user of a well-known, family name, age, etc.): a hash with some fields requires only a small amount of space to store, so you can store millions of objects in a small Redis instance. Hashes are primarily used to represent objects, they have the ability to store many objects, so you can use hashes for many other tasks.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/70/14/wKiom1WwwsHDVKiDAANoOXtjCU8738.jpg "title=" 8.png " Width= "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:320px; "alt=" Wkiom1wwwshdvkidaanooxtjcu8738.jpg "/>
Hmset command sets a multi-domain hash table, the Hget command gets the specified single field, and the Hgetall command gets all the information for the specified key. Hmget is similar to Hget, but returns an array of value.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/14/wKiom1WwwszR3mVhAAGpmZTJFRY036.jpg "title=" 9.png " Width= "height=" 184 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:184px; "alt=" Wkiom1wwwszr3mvhaagpmztjfry036.jpg "/>
It is also possible to perform separate operations on the table entries of the hash table as needed, such as Hincrby, (originally Birthyear is 1977, see previous image)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/70/11/wKioL1WwxMLRpZDaAAFbhEZZcFw578.jpg "title=" 10.png "alt=" wkiol1wwxmlrpzdaaafbhezzcfw578.jpg "/>
iv. Redis Unordered collection
redis A collection (set) is an unordered collection of strings . you can add, delete, and test elements with the time complexity of O (1) (regardless of how many elements in the collection are constants). The Redis collection has a satisfactory property that does not allow the same member to be included. Add the same element multiple times, and eventually there will be only one element in the collection. Actually says this means that there is no need to detect the presence of elements when adding elements. A very interesting thing about a Redis collection is that he supports some server-side commands to set operations from existing collections, so you can merge (unions), intersection (intersections) in a very short period of time, Find out the different elements (differences of sets).
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/11/wKioL1WwxS6jtX7OAAIFkK69tVA094.jpg "title=" 11.png "Width=" "height=" 176 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:176px; "alt=" Wkiol1wwxs6jtx7oaaifkk69tva094.jpg "/>
Sismember is used to see if the collection exists, and matches include the collection name and the number of elements. The match returns 1 successfully, and the matching failure returns 0.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/11/wKioL1WwxTiTPqSsAAFmsk3l4Ak870.jpg "title=" 12.png "alt=" wkiol1wwxtitpqssaafmsk3l4ak870.jpg "/>
Five, Redis ordered collection
A redis ordered set is very similar to a normal set, and is a collection of strings without repeating elements. The difference is that no member of an ordered set is associated with a score, which is used to sort the members of the collection in the same way as from the lowest score to the highest score. The members of the collection are unique, but the ratings can be duplicated. With an ordered set you can add, delete, and update elements at very fast speeds (O (log (N))). Because the elements are ordered, you can also quickly get a range of elements based on the score (score) or order (position). Accessing the intermediate elements of an ordered collection is also very fast, so you can use an ordered set as a smart list with no duplicate members. In an ordered set, you can quickly access everything you need: ordered elements, fast presence testing, quick access to the middle elements of the collection! In short, you can do many tasks that have extreme performance requirements by using ordered collections, and those tasks that use other types of databases are really hard to accomplish.
Zadd is similar to Sadd, but has one more parameter before the element, and this parameter is used for sorting. To form an ordered set.
View Collection Zrange is a collection of view positive orders, and Zrevrange is a collection that looks at the reverse order. 0 represents the first element of a collection, and 1 represents the penultimate element of the collection.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/70/14/wKiom1Www1yg-_mQAAS-YoQrVUY588.jpg "title=" 13.png "Width=" "height=" 480 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:480px; "alt=" wkiom1www1yg-_ Mqaas-yoqrvuy588.jpg "/>
Reference documents
Http://redis.io/topics/data-types-intro
Http://www.cnblogs.com/guanjinke/archive/2013/01/21/2870576.html
Http://www.linuxidc.com/Linux/2014-05/101544p2.htm
Redis data types