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
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
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
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:
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
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
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
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
Write a piece of pseudo code as follows:
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:
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
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.