Redis Basic Types

Source: Internet
Author: User

REmote DIctionary Server (Redis) is a Key-value storage system written by Salvatore Sanfilippo


    • String

you can set and get commands to create and retrieve strings. 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.
127.0.0.1:6379> set MyKey newval NX (nil) 127.0.0.1:6379> get MyKey "somevalue" 127.0.0.1:6379> 127.0.0.1:6379 > Set MyKey newval xxok127.0.0.1:6379> get MyKey "newval"

string is the basic type of redis, and you can do some interesting things about it, such as the adder:
127.0.0.1:6379> Set Counter 100ok127.0.0.1:6379> incr counter (integer) 101127.0.0.1:6379> incr counter ( Integer) 102127.0.0.1:6379> incrby counter (integer) 152

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.

Redis can use the Mset and MGET command to complete a one-time correspondence between multiple key-value, using the MGET command, Redis returns an array of value



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    • List

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:



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.


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.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    • 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.
127.0.0.1:6379> hmset user:1000 username Liubo Age 100//create record, equivalent to database table one record ok127.0.0.1:6379> hget user:1000 u sername//gets the value that records the field name corresponding to the value "Liubo" 127.0.0.1:6379> hget user:1000 Money "100"

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.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    • Set

Unordered collection

A Redis collection (set) is an unordered collection of strings. You can add, delete, and test elements with a time complexity of O (1), regardless of how many elements in the collection have a constant time complexity. 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. In fact, 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 perform set operations from existing collections, so you can merge (unions) in a very short time, intersection (intersections), and find different elements (differences of sets).



The Sadd command produces an unordered collection that returns the number of elements in the collection. Smember is used to view collections.

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.

127.0.0.1:6379> sismember myset 3 (integer) 1127.0.0.1:6379> sismember myset (integer) 0


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    • Zset

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.




use the Withscores parameter to return the record value.
127.0.0.1:6379> zrevrange Myzset 0 2  


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Redis Basic Types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.