search

Home  >  Q&A  >  body text

Redis 如何限制短信发送

暂时不考虑 ip,什么的限制,只考虑 手机号限制,

如何用redis做到限制,一个手机号,1分钟内最多发一条,一天内最多10条

各位大神有什么好方案,先谢!(崇拜脸)

迷茫迷茫2772 days ago943

reply all(6)I'll reply

  • 漂亮男人

    漂亮男人2017-04-27 09:05:05

    Requires two caches

    1. key name phone-busy, cached for 1 minute

    2. key name phone-send-count, cached for 1 day, +1 for each successfully sent message

    The process when sending is as follows:

    1. Determine whether phone-busy exists, if there is a direct error "sent too fast".

    2. 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

    3. Send SMS

    4. Write phone-busy, valid for 1 minute

    5. Write phone-send-count+1, the timestamp of 23:59:59 on the day of validity - the current timestamp

    reply
    0
  • PHPz

    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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你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`内

    reply
    0
  • 伊谢尔伦

    伊谢尔伦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;
    }

    reply
    0
  • PHP中文网

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-27 09:05:05

    Simple instructions:

    1. You need to have two keys

    2. A key that expires in 1 minute

    3. 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.

    reply
    0
  • Cancelreply