Seven Weapons of redis

Source: Internet
Author: User
Tags new set

The seven weapons used by Gu Long's pen are: changshengjian, peacock, biyu Dao, Duoqing ring, separation hook, Overlord gun, and fist, in this article, we plan to compare several usage methods of redis, such as strings, hashs, lists, sets, sorted sets, pub/sub, and transactions to seven weapons to explain the seven features of redis, and lists the applicable application scenarios.

Strings

The strings data structure is a simple key-value type. The value is actually not only a string, but also a number. With the strings type, you can fully implement the current memcached functions and improve efficiency. You can also enjoy redis's timed persistence, operation logs, replication, and other functions. In addition to get, set, incr, and decr operations like memcached, redis also provides the following operations:

  • Returns the string length.
  • Append content to string
  • Set and obtain a part of a string
  • Set and obtain a bit of a string)
  • Batch set the content of a series of strings
Hashs

In memcached, we often package some structured information into a hashmap, Which is serialized on the client and stored as a string value, such as your nickname, age, gender, and points, at this time, when you need to modify one of the items, you usually need to retrieve all the values for deserialization, modify the values of one item, and then serialize and store them back. This not only increases the overhead, but also does not apply to scenarios where concurrent operations are possible (for example, points must be modified for both concurrent operations ). The hash structure of redis allows you to modify only one attribute value just like updating an attribute in the database.

Lists

Lists is a linked list. I believe that anyone with a little knowledge about data structures should be able to understand its structure. Using the lists structure, we can easily implement the latest message ranking and other functions. Another application of lists is the message queue column. You can use the push operation of lists to store the task in lists, and then use the pop operation to retrieve and execute the task. Redis also provides APIs for operations on a certain segment of lists. You can directly query and delete the elements of a segment of lists.

Sets

Sets are a set. The concept of a set is a combination of non-repeated values. Using the sets data structure provided by redis, you can store some aggregated data. For example, in a Weibo application, you can store all the followers of a user in one collection, store all its fans in a set. Redis also provides operations such as intersection, union, and difference set for the set, which can be easily implemented, such as common concerns, common preferences, and friends of two degrees, you can also use different commands to choose whether to return the result to the client or save the result to a new set.

Sorted Sets

Compared with sets, sorted sets Add a weight parameter score, which enables the elements in the set to be sorted by score. For example, a sorted set that stores the scores of the entire class, the set value can be the student's student ID, while the score can be the score for the exam. This way, when the data is inserted into the set, the data has been naturally sorted. In addition, sorted sets can be used for weighted queues. For example, the score of a common message is 1 and that of an important message is 2, then, the worker thread can obtain the task in descending order of score. Give priority to important tasks.

Pub/sub

Pub/sub is literally publish and subscribe. In redis, you can set a key value for message publishing and message subscription, after a message is published on a key value, all clients that subscribe to it will receive the corresponding message. The most obvious usage of this function is to use it as a real-time messaging system, such as Common Instant chat and group chat functions.

Transactions

Who said nosql does not support transactions, although redis's transactions does not provide strict acid transactions (for example, if a string of commands submitted and executed using exec, the server goes down during execution, some commands are executed, but the rest are not executed. However, transactions provides the basic command packaging and execution function (when the server is normal, it can be ensured that a series of commands are executed in sequence, and other client commands are inserted in the middle for execution ). Redis also provides a watch function. You can watch a key and then execute transactions. During this process, if the value of watched is modified, then this transactions will find and reject the execution.

Well, this article will talk about it first. The above is just some of the usage methods of redis API, which can be described as the tip of the iceberg. redis is like a Swiss Army knife (or a more omnipotent Chinese army shovel ), it creates a series of data structures and APIs that are closer to application scenarios, with the aim of making storage design more direct based on application scenarios. More application scenarios and combined applications need to be explored by yourself. Or pay more attention to this site (nosqlfan. com). You can also find some interesting redis-based applications.

Seven Weapons of 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.