关于云原生:云原生应用架构设计与开发实战MKW

44次阅读

共计 1422 个字符,预计需要花费 4 分钟才能阅读完成。

download: 云原生利用架构设计与开发实战

复制下哉课程 ZYhttps://www.97yrbl.com/t-1389.html

@Component
public class RedisUtil {
 
    public static RedisUtil util;
 
    public RedisUtil(@Autowired  JedisPool jedisPool) {
        this.jedisPool = jedisPool;
        RedisUtil.util = this;
    }
 
    public static RedisUtil getInstance(){return util;}
 
 
    @Autowired
    private JedisPool jedisPool;
 
    /**
     * 向 Redis 中存值,永恒无效
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        try {jedis = jedisPool.getResource();
            return jedis.set(key, value);
        } catch (Exception e) {return "0";} finally {jedis.close();
        }
    }
 
    /**
     * 依据传入 Key 获取指定 Value
     */
    public String get(String key) {
        Jedis jedis = null;
        String value;
        try {jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {return "0";} finally {jedis.close();
        }
        return value;
    }
 
    /**
     * 校验 Key 值是否存在
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {return false;} finally {jedis.close();
        }
    }
 
    /**
     * 删除指定 Key-Value
     */
    public Long del(String key) {
        Jedis jedis = null;
        try {jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (Exception e) {return 0L;} finally {jedis.close();
        }
    }
 
    /**
     * 分布式锁
     * @param key
     * @param value
     * @param time 锁的超时工夫,单位:秒
     *
     * @return 获取锁胜利返回 "OK",失败返回 null
     */
    public String getDistributedLock(String key,String value,int time){
        Jedis jedis = null;
        String ret = "";
        try {jedis = jedisPool.getResource();
 
            ret = jedis.set(key, value, new SetParams().nx().ex(time));
            return ret;
        } catch (Exception e) {return null;} finally {jedis.close();
        }
    }
 
    public void pub(){jedisPool.getResource().publish("test","msg");
    }
 
}

正文完
 0