Here are 11 Web application scenarios where you can make the most of the Redis features and improve efficiency.
1. Display the latest list of items on the home page
Redis uses a cache of resident memory, which is very fast. Lpush is used to insert a content ID that is stored as a keyword in the head of the list. LTrim is used to limit the number of items in the list to 5000. If the user needs to retrieve more data than this cache capacity, then the request should be sent to the database.
2. Delete and filter
If an article is deleted, you can use Lrem to completely remove it from the cache.
3. Rankings and related issues
The rankings (leader board) are sorted by score. The Zadd command can directly implement this function, and the Zrevrange command can be used to score the top 100 users, Zrank can be used to get the user rankings, very straightforward and easy to operate.
4. By user voting and time sorting
It's like Reddit's list, and the score will change over time. Lpush and LTrim commands are combined to add an article to a list. A background task is used to get the list and recalculate the sorting of the list, which is used to populate the build list in a new order. Zadd Lists enable very fast retrieval, even for heavily loaded sites.
5. Overdue Project Processing
Use Unix time as a keyword to keep the list sorted by time. Search for Current_time and time_to_live to complete the arduous task of finding overdue items. Another background task uses the Zrange ... Withscores the query to delete the expired entries.
6. Counting
The use of various data statistics is very extensive, such as to know when to block an IP address. The Incrby command makes these easy to keep count by atomic increments; Getset is used to reset counters; The Expiration property is used to confirm when a keyword should be deleted.
7. Specific projects within a specific time period
This is a problem for a particular visitor and can be resolved by using the Sadd command for each page browse. Sadd does not add a member that already exists to a collection.
8. Real-time analysis of what is happening, for data statistics and prevention of spam, etc.
Using the Redis command, it is easier to implement spam filtering systems or other real-time tracking systems.
9.pub/sub
Maintaining the user's mapping of data in an update is a common task in the system. Redis's pub/sub feature uses the subscribe, unsubscribe, and publish commands to make this easier.
10. Queues
Queues are everywhere in the current programming. In addition to the push and pop type commands, Redis has a command to block queues, allowing one program to be added to the queue by another program at execution time. You can also do something more interesting, such as an RSS feed queue that spins updates.
11. Caching
The Redis cache is used in the same way as memcache.
Network applications cannot endlessly model wars, look at these Redis primitives commands, even though they are simple but powerful, and they can be made more unimaginable by combining them. Of course, you can write code specifically to do all of this, but Redis is obviously easier to implement.