美文网首页
redis实现延迟功能

redis实现延迟功能

作者: 周六不算加班 | 来源:发表于2018-07-24 16:43 被阅读126次
/**
 * 延迟10秒钟
 * @param phone
 * @return
 */
@Override
public  Object delay(final String phone){
    String redisKey = "SMS_LIMIT_"+phone;
    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
    Long count = operations.increment(redisKey,1);

    if (count == 1){
        redisTemplate.expire(redisKey,10,TimeUnit.SECONDS);
    }
    if (count >1){
        return "10秒内只能操作一次";
    }

    /**
     * 可以用来发短信等操作
     */
    this.set(phone,"bbb");
    return "操作成功";
}

相关文章

网友评论

      本文标题:redis实现延迟功能

      本文链接:https://www.haomeiwen.com/subject/grlymftx.html