博主首页

redis锁

public class RedisTag {


    /**
     *
     * @param tagName
     * @param type 1、开始2、结束
     * @param redisTemplate
     * @return
     */
    public static boolean tag(String tagName,String type, RedisTemplate<String,String> redisTemplate){
        if(StringUtils.eq("1",type)){
            Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(tagName, "1", 5, TimeUnit.MINUTES);
            return Optional.ofNullable(aBoolean).orElse(false);
        }else {
            Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(tagName, "1", 5, TimeUnit.SECONDS);
            Boolean aBoolean1 = Optional.ofNullable(aBoolean).orElse(true);
            if(!aBoolean1){
                try {
                    redisTemplate.delete(tagName);
                }catch (Exception ignored){
                }
            }
            return true;
        }
    }

 

posted @ 2023-11-30 10:28  笑~笑  阅读(8)  评论(0编辑  收藏  举报