What are the basic data structures of redis and what are their applications?

1. String - String 

String data structure is a simple key-value type, value can be not only String, but also numbers (when the number type
can be represented by Long, encoding is an integer, and the others are stored in sdshdr as characters. string). Using the Strings type can fully implement
the current Memcached functions and is more efficient. You can also enjoy the timing persistence of Redis (you can choose RDB mode or AOF mode), operation log and Replication and other functions. In addition to providing the same get, set, incr, decr and other operations as Memcached
, Redis also provides the following operations:
2. Hash - dictionary

Serialized and stored as a string value (usually in JSON
format), such as the user's nickname, age, gender, points, etc. At this time, when one of the items needs to be modified, it is usually necessary to take out the string (JSON), then deserialize it, modify the value of a certain item, and then serialize it into a string (JSON) and store it back. Simply modifying one property does so many things, and the consumption must be very large, and it is not suitable for some occasions where concurrent operations are possible (for example, two concurrent operations need to modify points). The
Hash structure of Redis allows you to modify only the value of an attribute just like updating an attribute in the database.

3. List -

List List is a linked list (redis uses a double-ended linked list to implement List), and I believe that anyone who has learned data structure knowledge should be able to understand its structure. Use List
structure, we can easily implement the latest news ranking and other functions (such as Sina Weibo's TimeLine). Another application of List is the message queue. You can use the *PUSH
operation of List to store the task in the List, and then the worker thread uses the POP operation to take out the task for execution. Redis also provides an
API for manipulating elements in a certain segment of List. You can directly query and delete elements in a certain segment of List.

4.

Set - set A set is a set, and the concept of a set is a combination of a bunch of non-repeating values. Using the Set data structure provided by Redis
, some aggregated data can be stored. For example, in a Weibo application, all followers of a user can be stored in a set, and all followers of a user can be stored in a set. Because Redis
provides collections with operations such as intersection, union, and difference, it is very convenient to implement functions such as common attention, common preferences, and second-degree friends. For all the above set operations, you can also Different commands can be used to choose whether to return the result to the client or save it to a new collection.

1. Common friends, second-degree friends
2. Using uniqueness, you can count all the independent IPs that visit the website
3. When friends recommend, find the intersection according to the tag, and
you can recommend if it is greater than a certain threshold
5. Sorted Set - ordered set

Compared with Sets, Sorted Sets adds a weight parameter score to the elements in the Set, so that the elements in the set can be
arranged in order by score. student number, and score
It can be its test score, so that when the data is inserted into the collection, it has already been sorted naturally. In addition, Sorted Sets can be used as a weighted queue. For example, the score of ordinary messages
is 1, and the score of important messages is 2. Then the worker thread can choose to obtain work tasks in the reverse order of the score. Prioritize important tasks.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326107943&siteId=291194637
Recommended