Five types of data for Redis

Source: Internet
Author: User
Tags hash redis

1.strings (String)

A) If you use only the string type in Redis and do not use the persistence feature of Redis, then Redis is very much like memcache;

b) When a numeric operation is encountered, it is automatically converted to a string, such as writing a number 1, read out will be the string 1;

c) The directive itself is atomic: incr, DECR and memecached in increment, decrement similar;

d) Application scenario: General count-Number of Weibo, number of fans;

2.lists (list)

A) The implementation of the lists data type is not an array but is implemented by the data structure of the linked list;

b) Using the lists structure, we can easily achieve the latest message ranking and other functions. Another application of the lists is Message Queuing, which can take advantage of the lists push operation to present the task in lists, and then the worker thread then takes the task out of execution with the pop operation;

c) Each child element is a string-type doubly linked list, which can be added or removed from the head or tail of the list via push and pop operations, so that the list can be used as a stack or as a queue;

3.sets (Unordered collection)

A) The so-called set is a combination of a bunch of distinct values, and there is no order;

b) In a microblog application, you can have a collection of all the followers of a user, and a collection of all their followers. Redis also 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, to all of the above collection operations, you can also use different commands to choose whether to return the results to the client or save set into a new collection.

4.sorted sets (ordered set)

A) compared with sets, Sorted sets adds a weight parameter score, so that the elements in the set can be arranged in order by score;

b) such as a sorted sets that stores the class's results, the collection value can be the student's school number, and score can be the test score, so that when the data is inserted into the collection, it has been a natural sort. You can use sorted sets to do a queue with weights, such as the score of the normal message is 1, the score of the important message is 2, and the worker thread can choose to get the work task in reverse order of score. Let important tasks take precedence.

c) Application scenario: Online game leaderboards, depending on the score you usually want:

I. List of top 100 high-score contestants

Ii. listing a user's current global rankings

5.hashes (hash)

A) in memcached, we often package some structured information into HashMap, which is stored as a string value after the client is serialized, such as the user's nickname, age, gender, integral, and so on, when it is necessary to modify one of these items, it is usually necessary to remove all values after deserialization, Modify the value of an item, and then serialize the store back. This not only increases the overhead, but also does not apply to some scenarios where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The hash structure of Redis allows you to modify only one item property value just as you would update a property in a database.

b) Application scenario: Store part of the change data, such as user information, session sharing;

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.