redis 包之间关系

一,redis客户端
1.jedis 2.redisson

二.redis操作方式
1.JedisCluster(客户端:jedis)(依赖:jedis)
2.redisson(客户端:redisson)
3.RedisTemplate (依赖:spring-data-redis)

三,依赖之前关系,见后面
以下包含两种:jedis redisson
依赖顺序为包含关系:上面包含下面依赖

四.序列化,desktop manager展示问题
展示**菱形黑符号,因为存的是二进制,只能展示二进制数据。
展示正常因为:1.存的时候就是String或json 2.单独加了template序列化配置类
1.JedisCluster 查询的是二进制
2.RedisTemplate 查询的是正常json

对比三个不同项目,redis存储方式。

1)JedisCluster序列化(我们自己封装)
  public boolean set(String key, Object value, Integer expiration) throws Exception {
        String result = "";
        long begin = System.currentTimeMillis();
        if (expiration > 0) {
            result = this.jedisCluster.setex(SafeEncoder.encode(key), expiration, this.serialize(value));
        } else {
            result = this.jedisCluster.set(SafeEncoder.encode(key), this.serialize(value));
        }

        long end = System.currentTimeMillis();
        this.logger.info("set key:" + key + ", spends: " + (end - begin) + "ms");
        return "OK".equalsIgnoreCase(result);
    }
protected byte[] serialize(Object o) {
    if (o == null) {
        return new byte[0];
    } else {
        Object var2 = null;

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(bos);
            os.writeObject(o);
            os.close();
            bos.close();
            byte[] rv = bos.toByteArray();
            return rv;
        } catch (IOException var5) {
            throw new IllegalArgumentException("Non-serializable object", var5);
        }
    }
}

2)RedisTemplate

redisTemplat.opsForValue().set("template",teacher);

类:AbstractOperations
public void set(K key, V value) {
    final byte[] rawValue = this.rawValue(value);
    this.execute(new AbstractOperations<K, V>.ValueDeserializingRedisCallback(key) {
        protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
            connection.set(rawKey, rawValue);
            return null;
        }
    }, true);
}

byte[] rawValue(Object value) {
    return this.valueSerializer() == null && value instanceof byte[] ? (byte[])((byte[])value) : this.valueSerializer().serialize(value);
}

最终保存接口:
public interface RedisStringCommands {

    @Nullable
    Boolean set(byte[] var1, byte[] var2);
3)ValueOperation
redisUtils.set(key, JSONObject.toJSONString(x), RedisEnum.DISTINCT_ORDER_NO.expired);

@Autowired
private ValueOperations<String, String> valueOperations;
public void set(String key, Object value, long expire) {
    this.valueOperations.set(key, JsonUtils.toJson(value));
    if (expire != -1L) {
        this.expire(key, expire, TimeUnit.SECONDS);
    }

}

类:DefaultValueOperations
@Override
public void set(K key, V value) {

       byte[] rawValue = rawValue(value);
       execute(new ValueDeserializingRedisCallback(key) {

              @Override
              protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
                     connection.set(rawKey, rawValue);
                     return null;
              }
       }, true);
}

jar依赖

1.使用JedisCluster
<!--jedis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.4.4</version>
</dependency>

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.4.7</version>
</dependency>

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>2.4.2</version>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.6.0-RC1</version>
</dependency>

<!-- redisson -->
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.15.3</version>
</dependency>

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-data-21</artifactId>
    <version>3.15.3</version>
</dependency>

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson</artifactId>
    <version>3.15.3</version>
</dependency>

 

上一篇:Redisson 源码初探(十一) CountDownLatch


下一篇:【分布式锁的演化】手撕redis分布式锁,隔壁张小帅都看懂了!