乐趣区

关于redis:jedis源码分析写数据

调用栈

sendCommand:100, Protocol (redis.clients.jedis) // 发送命令到 redis 服务器
sendCommand:85, Protocol (redis.clients.jedis)
sendCommand:115, Connection (redis.clients.jedis)

set:133, BinaryClient (redis.clients.jedis)
set:58, Client (redis.clients.jedis)
set:153, Jedis (redis.clients.jedis) // 写数据

redisDSTest:16, RedisDSTest (cn.hutool.db.nosql) // 利用类 - 入口
invoke0:-1, NativeMethodAccessorImpl (jdk.internal.reflect)
invoke:62, NativeMethodAccessorImpl (jdk.internal.reflect)
invoke:43, DelegatingMethodAccessorImpl (jdk.internal.reflect)
invoke:566, Method (java.lang.reflect)
runReflectiveCall:59, FrameworkMethod$1 (org.junit.runners.model)
run:12, ReflectiveCallable (org.junit.internal.runners.model)
invokeExplosively:56, FrameworkMethod (org.junit.runners.model)
evaluate:17, InvokeMethod (org.junit.internal.runners.statements)
evaluate:306, ParentRunner$3 (org.junit.runners)
evaluate:100, BlockJUnit4ClassRunner$1 (org.junit.runners)
runLeaf:366, ParentRunner (org.junit.runners)
runChild:103, BlockJUnit4ClassRunner (org.junit.runners)
runChild:63, BlockJUnit4ClassRunner (org.junit.runners)
run:331, ParentRunner$4 (org.junit.runners)
schedule:79, ParentRunner$1 (org.junit.runners)
runChildren:329, ParentRunner (org.junit.runners)
access$100:66, ParentRunner (org.junit.runners)
evaluate:293, ParentRunner$2 (org.junit.runners)
evaluate:306, ParentRunner$3 (org.junit.runners)
run:413, ParentRunner (org.junit.runners)
run:137, JUnitCore (org.junit.runner)
startRunnerWithArgs:68, JUnit4IdeaTestRunner (com.intellij.junit4)
startRunnerWithArgs:33, IdeaTestRunner$Repeater (com.intellij.rt.junit)
prepareStreamsAndStart:230, JUnitStarter (com.intellij.rt.junit)
main:58, JUnitStarter (com.intellij.rt.junit)

入口

应用程序的单元测试类

package cn.hutool.db.nosql;

import cn.hutool.db.nosql.redis.RedisDS;
import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore;
import org.junit.Test;
import redis.clients.jedis.Jedis;

@Slf4j
public class RedisDSTest {

    @Test
//    @Ignore
    public void redisDSTest(){final Jedis jedis = RedisDS.create().getJedis(); // 从 jedis 连接池获取一个连贯对象
        jedis.set("name","gzh"); // 通过连贯对象往 redis 服务器写数据
        String v = jedis.get("name"); // 读数据

        log.info("v={}",v);
    }
}

入参的值一开始是字符串 Jedis

入参的值是字符串

public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands, SentinelCommands, ModuleCommands {public String set(String key, String value) {this.checkIsInMultiOrPipeline();
  this.client.set(key, value);
  return this.client.getStatusCodeReply();}

编码 Client

把字符串编码为二进制数据

public class Client extends BinaryClient implements Commands {public void set(String key, String value) {this.set(SafeEncoder.encode(key), SafeEncoder.encode(value));
}

二进制客户端 BinaryClient

调用发送命令办法

public class BinaryClient extends Connection {public void set(byte[] key, byte[] value) {this.sendCommand(Command.SET //set 命令, new byte[][]{key, value} // 要写的值 );
}

发送命令 Connection

public class Connection implements Closeable {public void sendCommand(ProtocolCommand cmd //set 命令, byte[]... args) {
  try {this.connect(); // 校验连贯是否敞开
    Protocol.sendCommand(this.outputStream, cmd, args); // 发送命令,其实就是写数据到客户端,写什么数据呢?key value 这两个字符串。} catch (JedisConnectionException var6) {
    JedisConnectionException ex = var6;

    try {String errorMessage = Protocol.readErrorLineIfPossible(this.inputStream);
      if (errorMessage != null && errorMessage.length() > 0) {ex = new JedisConnectionException(errorMessage, ex.getCause());
      }
    } catch (Exception var5) { }

    this.broken = true;
    throw ex;
  }
}

协定层 Protocol

public final class Protocol {public static void sendCommand(RedisOutputStream os, ProtocolCommand command, byte[]... args) {sendCommand(os, command.getRaw(), args);
}
private static void sendCommand(RedisOutputStream os, byte[] command, byte[]... args) {
  try {os.write((byte)42);
    os.writeIntCrLf(args.length + 1);
    os.write((byte)36);
    os.writeIntCrLf(command.length);
    os.write(command); // 通知服务器以后用的是哪个命令操作?set 操作
    os.writeCrLf();
    byte[][] var3 = args; // 这个数组的值就是两个数据 key 和 value
    int var4 = args.length; // 数组的长度是 2

    for(int var5 = 0; var5 < var4; ++var5) { // 循环把 key 和 value 写到 redis 服务器
      byte[] arg = var3[var5];
      os.write((byte)36);
      os.writeIntCrLf(arg.length);
      os.write(arg);
      os.writeCrLf();}

  } catch (IOException var7) {throw new JedisConnectionException(var7);
  }
}

类继承图

总结

这个是纯 jedis 客户端,和 spring-data-redis/SpringTemplate 没什么关系。

其实就是基于 jedis 封装了一个工具类。

代码

开源我的项目 hutool
https://github.com/looly/hutool

文档
https://www.hutool.cn/docs/#/…

退出移动版