Home > Backend Development > PHP Tutorial > Introduction to redis counter method to prevent brushing orders

Introduction to redis counter method to prevent brushing orders

小云云
Release: 2023-03-17 22:06:01
Original
2677 people have browsed it

If we want to handle concurrency limits on interface requests, or make a security interception to prevent order fraud, for example, for an interface request, limit the total number of requests per second to 200, and wait if the number exceeds 200. One second, request again, here a redis is used as a counter mode to implement. This article mainly introduces to you the method and related code of redis to implement counter to prevent fraudulent orders. It has certain reference value and friends in need can learn about it.

Method to call redis:

INCR key

Increase the numerical value stored in key by one.

If key does not exist, the value of key will be initialized to 0 first, and then the INCR operation will be performed.

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

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

code:

redis> SET test 20
OK
redis> INCR test
(integer) 21
redis> GET test # 数字值在 Redis 中以字符串的形式保存
"21"
Copy after login

Implementation of counter

The counter is the atomic increment of Redis The most intuitive pattern that operations can implement, the idea is quite simple: every time an operation occurs, send an INCR command to Redis.

For example, in a web application, if you want to know the number of user clicks every day for a year, then you only need to use the user ID and related date information as keys, and execute it every time the user clicks on the page. One auto-increment operation is enough.

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) {
//设置有效期一s
$this->redis->expire($redisKey,1);//设置一s的过期时间
}
if (count > 200) {//防止刷单的安全拦截
return false;//超过就返回false
}
//后续处理
Copy after login

This simply implements the application of redis counter, and there are also the following methods:

The following are There are several ways to extend this simple pattern:

You can use INCR and EXPIRE in combination to achieve the purpose of counting only within the specified lifetime.
The client can atomically obtain the current value of the counter and clear the counter by using the GETSET command. For more information, please refer to the GETSET command.
Using other increment/decrement operations, such as DECR and INCRBY, users can increase or decrease the value of the counter by performing different operations. For example, these commands may be used for scorers in games.

Related recommendations:

Summary of usage of Redis function in php

Full record of Redis cluster construction

10 recommended content for performance counters

The above is the detailed content of Introduction to redis counter method to prevent brushing orders. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template