Jedis源碼解析——Jedis和BinaryJedis

一、基本信息

先來看看他們的類定義:java

public class Jedis extends BinaryJedis implements JedisCommands, MultiKeyCommands, AdvancedJedisCommands, ScriptingCommands, BasicCommands, ClusterCommands, SentinelCommands {
    ……
    }

public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, AdvancedBinaryJedisCommands, BinaryScriptingCommands, Closeable {
    ……
    }

Jedis繼承自BinaryJedis,兩者都實現了一系列的命令接口。redis

仔細看就能發現,這些接口幾乎是一一對應的:例如MultiKeyBinaryCommands和MultiKeyCommands,BinaryScriptingCommands和ScriptingCommands等等。從名字就能夠看出來他們之間的區別就是是不是binary(二進制)的,從他們的各自的方法中能夠找到依據。markdown

public interface MultiKeyBinaryCommands {
  Long del(byte[]... keys);

  List<byte[]> blpop(int timeout, byte[]... keys);

  List<byte[]> brpop(int timeout, byte[]... keys);

  List<byte[]> blpop(byte[]... args);

  List<byte[]> brpop(byte[]... args);

  Set<byte[]> keys(byte[] pattern);
  ……

}

public interface MultiKeyCommands {
  Long del(String... keys);

  List<String> blpop(int timeout, String... keys);

  List<String> brpop(int timeout, String... keys);

  List<String> blpop(String... args);

  List<String> brpop(String... args);

  Set<String> keys(String pattern);

……

}

這些接口命令中,只有BasicCommands是相同的,不存在是否是二進制相關的方法。由於該類中主要是redis的其餘操做,並不涉及具體數據類型的操做。簡單看幾個方法就知道了。socket

public interface BasicCommands {

  String ping();

  String quit();

  String flushDB();

  Long dbSize();

  String select(int index);

  String flushAll();

  String auth(String password);
……
}

二、結構關係

前面介紹完了兩個類之間的關係和實現的接口,雖然咱們日常使用以jedis居多,但其實精華都在binaryJedis中。函數

//真正的客戶端
 protected Client client = null;
 //用來區分是不是事務操做
  protected Transaction transaction = null;
  protected Pipeline pipeline = null;

//下面列舉了幾種比較典型的構造方法

  public BinaryJedis() {
    client = new Client();
  }

  public BinaryJedis(final String host) {
    URI uri = URI.create(host);
    if (uri.getScheme() != null && uri.getScheme().equals("redis")) {
      initializeClientFromURI(uri);
    } else {
      client = new Client(host);
    }
  }


  public BinaryJedis(final String host, final int port, final int connectionTimeout,
      final int soTimeout) {
    client = new Client(host, port);
    client.setConnectionTimeout(connectionTimeout);
    client.setSoTimeout(soTimeout);
  }

  public BinaryJedis(final JedisShardInfo shardInfo) {
    client = new Client(shardInfo.getHost(), shardInfo.getPort());
    client.setConnectionTimeout(shardInfo.getConnectionTimeout());
    client.setSoTimeout(shardInfo.getSoTimeout());
    client.setPassword(shardInfo.getPassword());
    client.setDb(shardInfo.getDb());
  }


  public BinaryJedis(final URI uri, final int timeout) {
    initializeClientFromURI(uri);
    client.setConnectionTimeout(timeout);
    client.setSoTimeout(timeout);
  }

構造函數中的操做,基本上都是建立Client,並初始化數據信息。那麼這個Client是個什麼呢?ui

這裏寫圖片描述

public class Client extends BinaryClient implements Commands {}

public class BinaryClient extends Connection {}

Tips:Client與BinaryClient的區別:BinaryClient是原生客戶端,而Client封裝性更好,也被叫作高級客戶端。spa

Connection類中主要是socket進行通訊,一個Connection已經就是一個最基礎的客戶端。code

另外增長各類協議層次的發送命令和收取結果的方法,都是經過Protocol類的操做RedisOutputStream和RedisInputStream完成的。封裝了輸入輸出流,來方便使用。繼承

public class Connection implements Closeable {

  private String host = Protocol.DEFAULT_HOST;
  private int port = Protocol.DEFAULT_PORT;
  private Socket socket;
  private RedisOutputStream outputStream;
  private RedisInputStream inputStream;
  ……
  }

主要方法:接口

一、connect()

public void connect() {
    if (!isConnected()) {
      try {
      //建立socket,設置相關參數
        socket = new Socket();
        socket.setReuseAddress(true);
        socket.setKeepAlive(true); // Will monitor the TCP connection is
        // valid
        socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to
        // ensure timely delivery of data
        socket.setSoLinger(true, 0); // Control calls close () method,
        // 鏈接
        socket.connect(new InetSocketAddress(host, port), connectionTimeout);
        socket.setSoTimeout(soTimeout);
        //輸入輸出
        outputStream = new RedisOutputStream(socket.getOutputStream());
        inputStream = new RedisInputStream(socket.getInputStream());
      } catch (IOException ex) {
        broken = true;
        throw new JedisConnectionException(ex);
      }
    }
  }

二、disconnect()

public void disconnect() {
    if (isConnected()) {
      try {
        outputStream.flush();
        socket.close();
      } catch (IOException ex) {
        broken = true;
        throw new JedisConnectionException(ex);
      } finally {
        IOUtils.closeQuietly(socket);
      }
    }
  }

三、isConnected()

public boolean isConnected() {
    return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected()
        && !socket.isInputShutdown() && !socket.isOutputShutdown();
  }
相關文章
相關標籤/搜索