J2Cache调用Redis的setnx和getset

private CacheChannel cacheChannel = SpringUtil.getBean(CacheChannel.class);
private static class Holder {
        private static J2CacheUtils single = new J2CacheUtils();
}
public static CacheProvider getL2Provider() {
        return Holder.single.cacheChannel.getL2Provider();
}
public static boolean setNxWithL2Redis(String region, String key, String val) {
        CacheProvider provider = getL2Provider();
        if (provider instanceof RedisCacheProvider) {
            RedisCacheProvider redisCacheProvider = (RedisCacheProvider) provider;
            RedisClient redisClient = redisCacheProvider.getRedisClient();
            Long result = redisClient.get().setnx((region + ":" + key).getBytes(), val.getBytes());
            return result == 0 ? false : true;
        } else {
            throw new RuntimeException("L2缓存不是Redis");
        }
}
public static String getSetWithL2Redis(String region, String key, String val) {
        CacheProvider provider = getL2Provider();
        if (provider instanceof RedisCacheProvider) {
            RedisCacheProvider redisCacheProvider = (RedisCacheProvider) provider;
            RedisClient redisClient = redisCacheProvider.getRedisClient();
            byte[] result = redisClient.get().getSet((region + ":" + key).getBytes(), val.getBytes());
            return new String(result);
        } else {
            throw new RuntimeException("L2缓存不是Redis");
        }
}

你可能感兴趣的:(J2Cache调用Redis的setnx和getset)