暂时不考虑 ip
,什么的限制,只考虑 手机号限制,
如何用redis
做到限制,一个手机号,1分钟内最多发一条,一天内最多10条
各位大神有什么好方案,先谢!(崇拜脸)
漂亮男人2017-04-27 09:05:05
Requires two caches
key name phone-busy, cached for 1 minute
key name phone-send-count, cached for 1 day, +1 for each successfully sent message
The process when sending is as follows:
Determine whether phone-busy exists, if there is a direct error "sent too fast".
Determine whether phone-send-count exists. If it exists, continue to detect whether it is equal to 10. If it exists and is equal to 10, an error "You cannot send text messages today" will be reported
Send SMS
Write phone-busy, valid for 1 minute
Write phone-send-count+1, the timestamp of 23:59:59 on the day of validity - the current timestamp
PHPz2017-04-27 09:05:05
Use mobile phone number + minute and mobile phone number + day as keys to limit the number of text messages sent in one minute and one day respectively
我想大声告诉你2017-04-27 09:05:05
Maximum one message can be sent within 1 minute, use minute and mobile phone number as key:min:201701041750:13888888888
一天内最多10条,用日期和手机号号为key:day:20170104:13888888888
//php demo
function check($phone){
if($redis->exists('min:'.date('YmdHi').':'.$phone) || $redis->get('day:'.date('YmdHi').':'.$phone) > 10){
reutrn false;
}
$redis->set('min:'.date('YmdHi').':'.$phone,1);
$redis->incr('day:'.date('Ymd').':'.$phone);
return true;
}
// 这样按分钟生成的key比较多,可以把手机号对应的分钟放`set`内
伊谢尔伦2017-04-27 09:05:05
Write a piece of pseudo code as follows:
String sendFrequencyKeyPrefix = "send_frequency_";
String dailySendLimitKeyPrefix = "daily_send_limit_";
public function sendMessage(String phoneNumber, String message){
if(redis.get((sendFrequencyKeyPrefix + phoneNumber))) {
return false;
}
int dailySendTotal = int(redis.get((dailySendLimitKeyPrefix + phoneNumber)));
if(dailySendTotal >= 10) {
return false;
}
// send message here
redis.set((sendFrequencyKeyPrefix + $phoneNumber), "", 60);
redis.set((dailySendLimitKeyPrefix + $phoneNumber), dailySendTotal + 1, 3600*24);
return true;
}
That’s it. You can set the one-minute or one-day sending limit into a configuration file, which is flexible and changeable.
Modified on January 5th;
My friend @Lowky here reminded me of a question, which is whether the daily limit is one day or 24 hours, because these two concepts are different. The main thing here is that there is a limit of 10 items per day, and the limit is released in the early morning every day. You should stop thinking about this every minute. The code is modified as follows:
String sendFrequencyKeyPrefix = "send_frequency_";
public function sendMessage(String phoneNumber, String message){
String dailySendLimitKeyPrefix = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
if(redis.get((sendFrequencyKeyPrefix + phoneNumber))) {
return false;
}
int dailySendTotal = int(redis.get((dailySendLimitKeyPrefix + phoneNumber)));
if(dailySendTotal >= 10) {
return false;
}
// send message here
redis.set((sendFrequencyKeyPrefix + $phoneNumber), "", 60);
redis.set((dailySendLimitKeyPrefix + $phoneNumber), dailySendTotal + 1, 3600*24);
return true;
}
PHP中文网2017-04-27 09:05:05
The first question can be achieved by setting the expiration time. As long as the mobile phone number exists in Redis, it means that it has not been 1 minute.
The second question is to change the counter every time it is sent and set the counter expiration time to one day
ringa_lee2017-04-27 09:05:05
Simple instructions:
You need to have two keys
A key that expires in 1 minute
A key that expires in 24 hours
It is processed according to the SMS business, which is based on the valid period, and is not settled at 0 o'clock.