Redis Data Type

Source: Internet
Author: User

TheArticleIs a translation of the official redis documentation

String (strings)

String is the most basic type of redis value. Redis string is binary secure, which means that a redis string can contain any type of data, such as a JPEG image or a serialized Ruby object. A string value can save up to MB of content. You can use the redis string to do something interesting. For example, you can:

    • When using the incr series (incr, decr, incrby) command, the string is used as the atomic counter.
    • Append a string using the APPEND Command.
    • Use the string as the random access Vector of getrange and setrange.
    • Encode a large amount of data in a small space, or use getbit and setbit to create a bloom filter supported by redis.

View available string commands for more information

List (lists)

The redis list is a simple string list sorted by insertion order. You can add a header (left) or tail (right) lpush command to insert a new element import header, while rpush inserts a new element import tail. when these two operations are executed on an empty key, a new list is created. Similarly, if A List Operation clears a list, the corresponding key is deleted from the key space. This is very convenient syntax, because they are called to use an empty list, just like they are called to use a nonexistent key value (CAN) as a parameter. Examples of some table-like operations and results:

Lpush mylist A # The current class table is ""

Lpush mylist B # The current list is "B", ""

Rpush mylist C # The current class table is "B", "A", "C" (This rpush is used)

A list can contain up to 232-1 elements (4294967295, each list contains more than 4 billion elements ). From the perspective of time complexity, the main feature of the redis list is that the insertion and deletion of elements at the beginning and end are fixed at a fixed time, even millions of inserts .. The access element at both ends of the list is very fast, but if you try to access the element in the middle of a very large list, it is very slow because it is an O (n) operation. You can use the redis list to do a lot of interesting things. For example, you can:

    • Create a timeline model in a social network, use lpush to add new elements to the user's timeline, and use lrange to receive some recently inserted elements.
    • You can use lpush and ltrim together to create a list that never exceeds the specified number of elements, but remember that it is the last n elements.
    • The list can be used as a message transmission primitive [Note: unclear meaning], for example, a well-known resque Ruby library used to create background work.
    • You can use the list to do more. This data type supports many commands, including blocking commands such as blpop. View the available list operation commands for more information.

Sets)

The redis set is an unordered string set. you can add, delete, and test the time complexity of O (1), regardless of the time complexity of all elements in the set. The redis set has satisfactory attributes that cannot contain the same members. After adding the same element multiple times, only one element exists in the set. In fact, this means that when adding an element, you do not need to check whether the element exists. A very interesting thing about a redis set is that it supports some server-side commands to perform set operations from the existing set, so you can merge (unions) in a very short time ), calculates the intersection to find different elements (differences of sets ). A set can contain a maximum of 232-1 elements (4294967295, each set contains more than 40 elements). You can use a set with many interesting things. For example, you can:

    • You can use a collection to track one (unique) thing and want to know all the independent IP addresses that access a blog article? Every time you process a page access, you simply use sadd. You can be sure that duplicate IP addresses will not be inserted.
    • Redis collections are good at expressing relationships. You can use the redis set to create a tagging system to present each tag. Next, you can use the sadd command to add all IDs of all objects with a given tag to a set to display this specific tag. Do you want to have the IDs of all objects with three different tags at the same time? Just use sinter.
    • Using the spop or srandmember command, you can use a set to randomly extract elements.
    • View the complete list of set commands for more information.

Hash (hashes)

Redis hashes is the ing between string fields and string values, so they are the perfect data type for displaying objects. (For example, a user with attributes such as name, surname, and age ):

@ CLI

Hmset User: 1000 username antirez password p1pp0 age 34

Hgetall User: 1000

Hset User: 1000 password 12345

Hgetall User: 1000

A hash with some fields (some here mean about one hundred) requires only a small space for storage, so you can store millions of objects in a small redis instance. Hash is mainly used to express objects. They have the ability to store many objects, so you can use hash for many other tasks. Each hash can store over 232-1 field-value pairs (over 4 billion). view the complete Hash command list for more information.

Sorted Sets)

An ordered redis set is very similar to a common set. It is a string set without repeated elements. The difference is that no member in an ordered set is associated with a score, which is used to sort the members in the set by the lowest score to the highest score. The Set members are unique, but the scores can be repeated. You can use an ordered set to add, delete, and update elements at a very high speed (O (log (n. Because the elements are ordered, you can quickly obtain the elements in a range based on the score or position. The intermediate elements used to access an ordered set are also very fast. Therefore, you can use an ordered set as an intelligent list Without Duplicate members. In an ordered set, you can quickly access everything you need: ordered elements, quick existence tests, and quick access to the intermediate elements of the set! In short, you can use an ordered set to complete many tasks that have extreme performance requirements. However, it is really difficult to use other types of databases for those tasks. You can use an ordered set:

    • display a ranking in a large online game. Once a new score is submitted, you can use the zadd command to update it. you can also use the zrange command to obtain top-level users. You can also use the zrank command to return the user's ranking in the ranking based on the user name. You can also use zrank and zrange to display all users with the same score as the given user. All these operations are fast.
    • Ordered Sets are often used to index data stored in redis. For example, if you have many hashes to represent users, you can use an ordered set. The age field of the elements in this set is used as a score, and ID as the value. Therefore, using the zrangebyscore command is insignificant and can quickly receive all users of a given age group.
    • An ordered set may be the most advanced redis data type. Therefore, take some time to view the complete list of Ordered Set commands to find out what you can do with redis.
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.