In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Recently, as double 11 is coming, companies need to deal with concurrency restrictions on interface requests, or do a security interception to prevent brushing:
For example, an API request that limits the total number of requests per second to 200, waits for more than 200, and waits for another request the next second. Here, a redis is used as a counter mode.
Call the method of redis:
INCR key
Increments the numerical value stored in key by one.
If key does not exist, 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 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 values save "21" as strings in Redis
Realization of counter
The counter is the most intuitive mode that can be achieved by Redis's atomic self-increment operation, and its idea is quite simple: whenever an operation occurs, send an INCR command to Redis.
For example, in a web application, if you want to know the number of clicks per day of the year, you can simply use the user's ID and related date information as keys and perform a self-increment operation each time the user clicks on the page.
For example, if the user name is peter and the 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 the expiration time of one s > redis- > expire ($redisKey,1); / / set the expiration time of one second} if (count > 200) {/ / to prevent the security interception of brushing from exceeding return false;//, return false} / / for subsequent processing
This simply realizes the application of redis counter, in addition, there are the following methods:
There are several ways to extend this simple pattern:
You can use a combination of INCR and EXPIRE to achieve the goal of counting (counting) only within a specified lifetime.
The client can atomically get the current value of the counter and clear the counter by using the GETSET command. For more information, please refer to the GETSET command.
With other add / subtract operations, such as DECR and INCRBY, users can increase or decrease the value of counters by performing different operations, such as these commands may be used by a scoreboard in a game.
Summary
The above is the whole content of this article on the implementation of redis counter-prevent brushing method, I hope to be helpful to you. Interested friends can continue to refer to this site: Redis master-slave synchronous parsing, Java programming redisson to achieve distributed lock code examples, a brief description of the difference between Redis and MySQL, etc., if you have any questions, you can leave a message at any time, the editor will reply you in time. Thank you for your support to this site!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.