Redis application scenarios)

Source: Internet
Author: User
ArticleDirectory
    • 1. Operations for retrieving the latest n data
    • 3. Applications requiring precise expiration time setting
    • 5. uniq operation to obtain the values of all data records in a certain period of time
    • 7. Build a Real-Time Message System Using pub/sub

Undoubtedly,RedisWe have created a new data storage concept. Using redis, we don't have to focus on how to put elephants in the refrigerator in the face of monotonous databases, instead, redis uses flexible data structures and data operations to build different refrigerators for different elephants. Hope you like this metaphor.

The following is a new article by @ antirez, author of redis, which describes some of the most suitable redisApplicationScenarios:

1. Operations for retrieving the latest n data

For example, for the latest articles on your website, we can place the latest 5000 comment IDs in the list set of apsaradb for redis, and retrieve the parts beyond the set from the database.

    • Run the lpush latest. Comments <ID> commandListSet to insert data
    • Use the ltrim latest. Comments 0 5000 command to save only the last 5000 IDs.
    • Then we can use the following logic (pseudoCode)
Function get_latest_comments (START, num_items): id_list = redis. lrange ("latest. comments ", start, start + num_items-1) If id_list.length <num_items id_list = SQL _db (" select... order by time limit... ") end return id_listend

If you have different filtering dimensions, such as the newest n entries of a specific category, you can create another list based on this category and store only IDs. redis is very efficient.

2. Rankings Application, Top N operations

The difference between this requirement and the above requirement is that the previous operation takes the time as the weight, and this is based on a certain condition as the weight, such as sorting by the number of top times. At this time, we need ourSorted setSet the value you want to sort to sorted.SetTo set the specific data to the corresponding value. You only need to execute a zadd command each time.

3. Applications requiring precise expiration time setting

For example, you can set the score value of the sorted set as the timestamp of the expiration time, so you can simply sort the expiration time and regularly clear the expired data, in addition to clearing expired data in redis, You can regard the expiration time in redis as an index of the data in the database, and use redis to find out which data needs to be deleted after expiration, then, the corresponding records are precisely deleted from the database.

4. Counter Application

Redis commands are atomic. You can easily use the incr and decr commands to build a counter system.

5. uniq operation to obtain the values of all data records in a certain period of time

The Set Data Structure of redis is the most suitable. You only need to constantly throw the data to the set. Set indicates the set, so it will automatically remove the weight.

6. Real-Time System, Anti-Spam System

With the set function mentioned above, you can know whether an end user has performed an operation, find a set of operations, and perform analysis, statistics, and comparison. You can't do it.

7. Build a Real-Time Message System Using pub/sub

Redis pub/sub systems can be used to build real-time messaging systems, for example, many examples of real-time chat systems built using pub/Sub.

8. Build Queue System

You can use list to build a queue system, and use sorted set to build a queue system.Priority.

9. Cache

This does not need to be said. The performance is better than mem.CacheD. More diversified data structures.

Source: antirez.com

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.