The call stack

SendCommand :100, Protocol (redis.clients.jedis) // sendCommand to redis server 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) // Make data dreary :16, make data dreary (cn.hutool.db.nosql) 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)Copy the code

The entrance

Unit test classes for the application

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(); // Get a connection object jedis.set("name"," GZH ") from the jedis pool; V = jedis.get("name"); // Write data to redis server via connection object. Log.info ("v={}",v); }}Copy the code

The value of the input parameter starts with the string Jedis

The value of the input parameter is a string

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();
}
Copy the code

Coding the Client

Encode a string into binary data

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

BinaryClient BinaryClient

Call the send command method

public class BinaryClient extends Connection { public void set(byte[] key, Byte [] value) {this.sendCommand(command-set // SET Command, new byte[][]{key, value} // The value to write); }Copy the code

Send the command Connection

Public class Connection implements Closeable {public void sendCommand(ProtocolCommand CMD //set command, byte[]... args) { try { this.connect(); SendCommand (this.outputStream, CMD, args); // Write data to the client. Key value are two strings. } 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; }}Copy the code

The Protocol layer Protocol

public final class Protocol {
public static void sendCommand(RedisOutputStream os, ProtocolCommand command, byte[]... args) {
  sendCommand(os, command.getRaw(), args);
}
Copy the code
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); // Tell the server which command operation is currently used, such as set operation os.writecrlf (); byte[][] var3 = args; Int var4 = args.length; // Array length is 2 for(int var5 = 0; var5 < var4; Byte [] arg = var3[var5]; byte[] arg = var3[var5]; os.write((byte)36); os.writeIntCrLf(arg.length); os.write(arg); os.writeCrLf(); } } catch (IOException var7) { throw new JedisConnectionException(var7); }}Copy the code

Class diagram

conclusion

This is a pure Jedis client and has nothing to do with spring-data-redis/RedisTemplate.

Basically, a utility class is wrapped around Jedis.

code

Open source project Hutool github.com/looly/hutoo…

Document www.hutool.cn/docs/#/db/N…