11 Types of Redis Web application scenarios

Source: Internet
Author: User
Tags redis
11 Types of Redis Web application Scenarios2015-04-07 10:46 Anonymous Gray's blog size: T | T

Some Redis primitive commands, such as Lpush, LTrim, and Lrem, can be used to help developers accomplish the tasks they need--tasks that are difficult or slow in traditional database storage. This is a very useful and practical article. So how do you accomplish these tasks in your framework?

Ad:51cto Technology Salon | Give apps an amazing interaction and experience >>

Some Redis primitive commands, such as Lpush, LTrim, and Lrem, can be used to help developers accomplish the tasks they need--tasks that are difficult or slow in traditional database storage. This is a very useful and practical article. So how do you accomplish these tasks in your framework?

Here are 11 web scenarios where you can take advantage of Redis's features to improve efficiency.

1. Display the latest list of items in 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 list header. LTrim is used to limit the maximum number of items in a list to 5000. If the amount of data that the user needs to retrieve exceeds this cache capacity, then the request needs to be sent to the database.

2. Delete and filter

If an article is deleted, you can use Lrem to completely erase it from the cache.

3. Leaderboards and related issues

Leaderboards (leader board) are sorted by score. The Zadd command can implement this function directly, and the Zrevrange command can be used to get the top 100 users according to the score, Zrank can be used to get the user rank, very straightforward and easy to operate.

4. Sort by user poll and time

This is like Reddit's leaderboard, where scores change over time. The Lpush and LTrim commands are combined to add the article to a list. A background task is used to get the list and recalculate the sorting of the list, and the Zadd command is used to populate the build list in the new order. Lists can be retrieved very quickly, even on heavily loaded sites.

5. Overdue Item 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 it easy to keep count by atomic increment; The getset is used to reset the counter; The Expiration property is used to confirm when a keyword should be deleted.

7. Specific items during a given time

This is a problem for a particular visitor and can be resolved by using the Sadd command for each page view. 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 Primitives command makes it easier to implement spam filtering systems or other real-time tracking systems.

9.pub/sub

Maintaining user 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 ubiquitous in the current programming. In addition to the push and pop type commands, Redis also has commands 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.

Web applications cannot endlessly model wars, look at these redis primitive commands, even though they are simple but powerful and combine them to make it even more unimaginable. Of course, you can write code specifically to do all of this, but Redis is obviously easier to implement.

Blog Source: Http://my.oschina.net/HeAlvin

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.