美文网首页
Redis - increment 递增方法 | 处理防重复和并

Redis - increment 递增方法 | 处理防重复和并

作者: xiari1991 | 来源:发表于2020-12-05 22:09 被阅读0次

    使用场景

    • 1.有时因为网路原因,在保存或其他操作时会发生重复提交问题
    • 2.针对秒杀、抢购、多个用户同时下单的情况(不过redis针对秒杀只是其中的一环)
    long count = redisTemplate.opsForValue().increment(key, delta);
    if (count == 1) {
        rredisTemplate.opsForValue().set(key, tranRepeatTime);
    }else if (count > 2) {
        long timeOut = redisTemplate.opsForValue().getExpire(key);
        if (timeOut != tranRepeartTime) {
            redisTemplate.opsForValue().set(key, tranRepeatTiime);
       }
    }
    if (count > 1) {
          过期处理。。。
    }
    

    相关文章

      网友评论

          本文标题:Redis - increment 递增方法 | 处理防重复和并

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