Introduction to 11 Web application scenarios of Redis

  • 2020-05-13 03:47:27
  • OfStack

Here is a list of 11 Web application scenarios where you can take full advantage of Redis's features to increase efficiency.

1. Display the latest list of items on the home page

Redis USES a cache that is resident in memory and is very fast. LPUSH is used to insert a piece of content, ID, stored as a keyword in the header of the list. LTRIM is used to limit the number of items in the list to a maximum of 5000. If the user needs to retrieve more data than this cache capacity, then the request needs to be sent to the database.

2. Delete and filter

If an article is deleted, LREM can be used to clean it out of the cache completely.

3. Leaderboards and related issues

Leaderboards (leader board) are ranked by score. The ZADD command can directly implement this function, while the ZREVRANGE command can be used to obtain the top 100 users according to the score, and the ZRANK command can be used to obtain the user ranking, which is very direct and easy to operate.

4. Rank by user vote and time

It's like the Reddit leaderboard, where the score changes over time. Combine the LPUSH and LTRIM commands to add the article to a list. A background task is used to get the list and recalculate the order of the list, and the ZADD command is used to populate the generated list in the new order. Lists can be retrieved very quickly, even for heavily loaded sites.

5. Deal with expired items

Use unix time as a keyword to keep the list sorted by time. current_time and time_to_live are retrieved to complete the difficult task of finding expired items. Another background task USES ZRANGE... WITHSCORES queries to delete expired entries.

6. Count

The use of various data statistics is very extensive, such as wanting to know when to block an IP address. The INCRBY command makes this easy, keeping the count by atomic increment; GETSET is used to reset the counter; The expiration property is used to determine when a keyword should be deleted.

7. A specific project at a specific time

This is a visiter-specific problem that can be solved by using the SADD command for each page viewed. SADD does not add an existing member to a collection.

8. Real-time analysis of what is happening, used for data statistics and prevention of spam, etc

Using the Redis primitive command makes it easier to implement a spam filtering system or other real-time tracking system.

9.Pub/Sub

Keeping the user's map of the data in the update is a common task in the system. Redis's pub/sub functionality makes this easier by using the SUBSCRIBE, UNSUBSCRIBE, and PUBLISH commands.

10. The queue

Queues are everywhere in current programming. In addition to commands of the push and pop types, Redis also has commands to block queues so that one program can be added to the queue by another program at execution time. You can also do something more interesting, such as a rotating updated RSS feed queue.

11. The cache

The Redis cache is used in the same way as memcache.

Web applications can't endlessly wage model wars. Look at these simple but powerful primitive commands from Redis, and you can't imagine what you can accomplish when you combine them. Of course, you could write code specifically to do all of this, but Redis is much easier to implement.


Related articles: