Redis Learning Notes-application scenarios

Source: Internet
Author: User
Tags repetition

Recently looking at the Redis Getting Started guide, now talk about your own understanding of your learning situation.

String type (String)

The string type is the most basic type in Redis and can store arbitrary forms of strings, including binary data. A picture can also be stored as a string type. Note The string type key allows the stored data maximum capacity to be 512M.

The Strings data structure is a simple key-value type, and value is not only a string, it can also be a number. In addition to operations such as GET, set, INCR, DECR, Redis also provides the following actions:

Gets the string length;
Append content to a string;
Sets and gets a section of the string;
Sets and gets the string of one (bit);
Bulk set the contents of a series of strings;

So string types are very handy for saving strings and counting. Take a blog: to achieve the statistics of the article visits, generate self-increment ID, how many times, how many times, how many times to like, how many times, how many friends and so on, including the contents of the storage of the article only need to use the string type can be achieved.

Hash type (hash)

Hash is hash, hash type is used to store such as user's nickname, age, gender, integration, etc. is very convenient, can achieve only O (1) complexity. It is also very convenient to modify these contents.

For example: In a relational database, if you want to store a car object, the storage structure is shown in the following table:

If you want to increase the production date attribute for a car with an ID of 1, you need to change the data table to the structure shown in the following table:

The date field is redundant for two records with IDs 2 and 3. It is conceivable that when different records require different attributes, the number of fields in the table becomes more and more difficult to maintain.

There is no such situation in Redis. Of course, Redis seems to me to be a big waste, as in the example above: if the number of cars is too much, then each car has a color, name, price these characters need to be saved, although there is a way to simplify the key name is also very wasteful AH.

In fact, there is a question: Redis above examples are a layer of relational model, that multi-layered when redis is the multilayer decomposition?

List types (lists)

List is implemented with a linked list, and it is a doubly linked list. In this case it is obvious that inserting and deleting at both ends is very convenient O (1), so the list provides operations for basic operations Lpush, Rpush, Lpop, Rpop.

It is not difficult to see the list is easy to implement stacks and queues.

Based on these reasons: list is mainly used to take the latest n data operations, such as social networking sites, the latest news, the latest updated blog These use list implementation can do more with less.

One problem has always been unclear: The book says that the list is an ordered list of strings, so-called order is the push time in succession??

Collection type

The first thing we should notice in the collection is: Store The key that is not duplicated, and note that it is the only meaning of repetition.

We should also note that the collection is the value of the empty hash list implementation, hehe, then in fact, he realized that the principle of non-repetition is the use of hash list (hash) key value (key) unique characteristics.

Since he is a special form of the hash table, the properties of the hash table for quick access to--o (1) are also present in the collection. So the time complexity of accessing the collection is O (1).

All right, let's see where the collection is mainly used.

Uniq the characteristics of the--uniq operation, to obtain a period of time all data row weight value;

Simple and brutal and cross-cutting-for example, in a microblog application, you can have a collection of all the followers of a user, and a collection of all their fans. Redis provides for the collection of intersection, set, difference sets and other operations, can be very convenient to achieve such as common concern, common preferences, two-degree friends and other functions, for all the above set operation, you can also use different commands to choose whether to return the results to the client or save set into a new collection;

Ordered collection

Ordered sets are sorted by the high or low of a certain score.

An ordered set is achieved by using a hash table and a skip list.

The main use: such as a storage class students sorted sets, the collection of value can be the student's school number, and score can be its test scores, so in the data insertion set, it has been a natural sort. This is: Leaderboard app, take top n action.

Also can use sorted sets to do with the weight of the queue, such as the score of the ordinary message is 1, the important message of the score is 2, and then the worker can choose to score in reverse order to obtain work tasks, so that important tasks priority execution. This is the legendary: build a prioritized queue system.

Applications that require precise expiration time-if you can set the score value of the sorted set mentioned above to the time stamp of the expiration time, you can simply sort through the expiration time and periodically purge out-of-date data, not just the stale data in Redis, You can think of this expiration time in Redis as an index to the data in the database, use Redis to find out what data needs to be deleted, and then delete the corresponding records from the database exactly.

Other of

Pub/sub build real-time message system--redis PUB/SUB system can build real-time message system, such as many use pub/sub built real-time Chat system example, I think QQ message inside @all can use this realization;

Other high on the use of the words you may wish to refer to: http://www.csdn.net/article/1970-01-01/2817107

Redis Learning Notes-application scenarios

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.