Introduction to the Redis counter prevent brush order method

Source: Internet
Author: User
If we want to be on the interface request, do a concurrency limit processing, or do a protection against the flash of the security interception, such as an interface request, limit the total number of requests per second is 200 times, more than 200 times wait, wait for the next second, again request, here to use a Redis as a counter mode to achieve. This article mainly introduced the Redis implementation counter to prevent brush the method and the related code, has certain reference value, needs the friend to understand next.

Methods for calling Redis:

INCR Key

Increase the numeric value stored in key by one.

If key does not exist, then the value of key is initialized to 0 before performing the INCR operation.

If the value contains the wrong type, or if the value of the string type cannot be represented as a number, an error is returned.

This is a string-specific operation because Redis does not have a dedicated integer type, so the string stored in key is interpreted as a decimal 64-bit signed integer to perform the INCR operation.

Code

redis> SET Test 20okredis> INCR Test (integer) 21redis> GET Test # numeric value save "21" as a string in Redis

Implementation of the counter

The counter is the most intuitive mode that Redis's atomic auto-increment operation can achieve, and its idea is fairly simple: whenever an operation occurs, a INCR command is sent to Redis.

For example, in a Web application, if you want to know the user's daily clicks in a year, just use the user ID and the relevant date information as the key, and perform a self-increment action every time the user clicks the page.

For example, the user name is Peter, click Time is March 22, 2012, then execute the command INCR peter::2012.3.22.

$redisKey = "Api_name_" + $api; $count = $this->redis->incr ($redisKey); if ($count = = 1) {//Set validity period one s$this->redis-& Gt;expire ($redisKey, 1);//set an S expiration Time}if (Count > 200) {//Prevent the safe interception of brush orders return false;//more than return false}//subsequent processing

This simple implementation of the Redis counter application, in addition to the following methods:

Here are some ways to extend this simple pattern:

You can use INCR and EXPIRE in combination to achieve the purpose of counting (counting) only for the specified time-to-live.
The client can atomically get the current value of the counter and clear the counter by using the Getset command, for more information refer to the Getset command.
With other self-increment/decrement operations, such as DECR and Incrby, users can increase or decrease the value of the counter by performing different actions, such as the use of these commands in the game's scoreboard.

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.