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)

jedis源码分析-写数据_第1张图片

入口

应用程序的单元测试类

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源码分析-写数据_第2张图片

总结

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

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

代码

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

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

你可能感兴趣的:(redis,jedis)