Counter pattern instance of Redis usage pattern _redis

Source: Internet
Author: User
Tags hash redis

Redis is currently the NoSQL field of the popular fried chicken, it is like a Swiss Army knife, small, sharp, practical, especially suitable for the use of traditional relational database difficult to solve problems. Intend to write a series of redis use patterns of the article, in-depth summary of Redis common use patterns for everyone's reference.

Common Summary Counters

Summary count is a common feature of the system, such as the number of registered users, the total number of visits to the site, and so on. Summary counters can be implemented using the basic data types provided by Redis, and additional operations are implemented through the INCR command.

For example, the number of registered users, the basic operation commands are as follows:

Copy Code code as follows:

# Get registered User number
Get Total_users
# Number of registered users increased by one person
INCR total_users

Counter that is summarized by time

Usually count also by time statistics, such as the number of registered users need to daily statistics, processing method is relatively simple, the date into the counter key can be.

or a registered User Count example, the basic operation commands are as follows:

Copy Code code as follows:

# Assuming Operation 2014-07-06 data
# Get registered User number
Get total_users:2014-07-06
# Increased number of registered users in 2014-07-06
INCR total_users:2014-07-06
# set 48-hour Expiration 172800 = 48 * 60 * 60
Expire total_users:2014-07-06 172800

Setting a 48-hour expiration time for the counter is to save the space for the counter, after all, Redis is a memory database, you can perform a task before the expiration of the counter into the relational database.

Speed control

Speed control is also a common use of redis, such as having an API service, want to control each IP per second request no more than 10 times, you can use IP and time seconds as key to set a counter to achieve control, pseudo code as follows:

Copy Code code as follows:

# The maximum number of requests per second
Max_requests_per_second = 10

# Check IP Request restrictions
# @param IP
# @raise over limit, throw RuntimeError exception

def check_request_limitation_for_ip (IP)
Time_tick = Time.now.to_i
Key = "#{ip}:#{time_tick}"
num = $redis. Get (key). to_i
If num > Max_request_per_second
Raise ' too many requests '
Else
$redis. INCR (Key)
$redis. Expire (key, 10)
End
End

Maintaining a large number of counters with a Hash data type

Sometimes need to maintain a large number of counters, such as the number of views per forum topic, such as each user access page number, because the forum theme and user base may be very large, directly based on the forum theme or user ID generated counters, the use of Redis resources is still considerable, at this time can be used Hash The data type compresses the required resources.

For example, the corresponding forum topic view count, can be by the pattern

Copy Code code as follows:

Key:topic:<topic_id>:views
Value:view count (integer)

Convert to Mode:

Copy Code code as follows:

Key:topic:views
Value:hash
Hash key: <topic_id>
Hash value:view count (integer)

Summary: The use of Redis to implement counters, can be simple and efficient implementation of a variety of counting functions.

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.