Counter pattern instance of Redis's usage pattern
- 2020-05-07 20:38:45
- OfStack
Redis is the most popular Fried chicken in NoSQL. It is like a Swiss army knife. It is small, sharp and practical. I plan to write a series of articles on Redis usage patterns, which will provide an in-depth summary of common Redis usage patterns for your reference.
common summary counter
Summary counting is a common function of the system, such as the website usually needs to count the number of registered users, the total number of website visits and so on. Summary counters can be implemented using the basic data types provided by Redis, and increment operations can be implemented through the incr command.
For example, the number of registered users, the basic operation command is as follows:
# Get the number of registered users
get total_users
# Increase of registered users 1 position
incr total_users
counter by time summary
Counting is usually done by time. For example, the number of registered users needs to be counted by day.
Or the registered user count example, the basic operation command is as follows:
# Assume that the operating 2014-07-06 data
# Get the number of registered users
get total_users:2014-07-06
# 2014-07-06 Increase of registered users 1 position
incr total_users:2014-07-06
# Set up the 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 space for the counter. After all, redis is an in-memory database, so you can perform one task before expiration to store the counter in a relational database.
speed controls
Speed control is also one of the common counting purposes of Redis 1. For example, there is an API service, and you want to control the number of requests per second per IP not more than 10 times. You can use IP and time seconds as key to set a counter to achieve control.
# Maximum number of requests per second
MAX_REQUESTS_PER_SECOND = 10
# check ip Request limits
# @param ip
# @raise Over the limit, throw RuntimeError abnormal
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
maintains a large number of counters, , using the Hash data type
Sometimes need to maintain a large number of counters, such as every BBS theme 1 check number, each user to access the page number 1, for example, because the BBS topics and user base can be large, directly based on BBS theme or user ID generate counter, occupy or considerable Redis resources, then can use Hash data type compress the resources needed.
For example, the view count for the corresponding forum topic can be set by mode
key: topic:<topic_id>:views
value: view count (integer)
Convert to mode:
key: topic:views
value: hash
hash key: <topic_id>
hash value: view count (integer)
Conclusion: with Redis counter, various counting functions can be realized simply and efficiently.