Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/123.html?1455853785
Redis is the most popular deep-fried chicken in the NoSQL world, and it is a small, sharp, and practical, especially suited to solving problems that are difficult to solve with traditional relational databases. intends to write a series of redis usage patterns, in-depth summary of Redis common usage patterns for your reference.
Common summary counters ?
Summary count is a common function of the system, such as the number of registered users, website total number of visits and so on. Using the basic data types provided by Redis, you can implement summary counters and increase operations with the INCR command.
For example, the number of registered users, the basic Operation command is as follows:
The code is as follows:
# get the number of registered users
Get Total_users
# Number of registered users increase one bit
INCR total_users
Counters that are aggregated by time
Usually counting is also time-based statistics, such as the number of registered users need daily statistics, processing method is relatively simple, the date into the counter key can be.
Or an example of registering a user count, the Basic Operations command is as follows:
The code is as follows:
# Assume Operation 2014-07-06 data
# get the number of registered users
Get total_users:2014-07-06
# 2014-07-06 Increased number of registered users
INCR total_users:2014-07-06
# set 48-hour expiration Time 172800 = 48 * 60 * 60
Expire total_users:2014-07-06 172800
Setting a 48-hour expiration time for the counter is to save the counter space, after all, Redis is an in-memory database and can perform a task before expiration to store the counter in a relational database.
Speed control
Speed control is also a common use of Redis, such as an API service, want to control every IP request per second not more than 10 times, you can use the IP and time seconds as a key to set a counter to achieve control, pseudo-code as follows:
The code is as follows:
# Maximum number of requests per second
Max_requests_per_second = 10
# Check IP request limits
# @param IP
# @raise exceeds limit, throws 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 using Hash data types
Sometimes you need to maintain a large number of counters, such as the number of views of each forum theme, such as each user visits the page number, because the forum theme and user base may be large, directly based on the forum theme or user ID generated counter, the use of Redis resources is still considerable, then you can use Hash The data type compresses the required resources.
For example, the corresponding forum theme view count, can be by the mode
The code is as follows:
Key:topic:<topic_id>:views
Value:view count (integer)
Convert to Mode:
The code is as follows:
Key:topic:views
Value:hash
Hash key: <topic_id>
Hash value:view count (integer)
Summary: Using Redis to implement counters, you can easily and efficiently achieve a variety of counting functions
Example of a counter pattern for Redis usage mode