1、基本信息
先來看看他們的類定義:
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,二者都實現了一系列的命令接口。
仔細看就能發現,這些接口幾乎是一一對應的:例如MultiKeyBinaryCommands和MultiKeyCommands,BinaryScriptingCommands和ScriptingCommands等等。從名字就可以看出來他們之間的區別就是是否是binary(二進制)的,從他們的各自的方法中可以找到依據。
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的其他操作,並不涉及具體數據類型的操作。簡單看幾個方法就知道了。
public interface BasicCommands {
String ping();
String quit();
String flushDB();
Long dbSize();
String select(int index);
String flushAll();
String auth(String password);
……
}
2、結構關係
前面介紹完了兩個類之間的關係和實現的接口,雖然我們平常使用以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是個什麼呢?
public class Client extends BinaryClient implements Commands {}
public class BinaryClient extends Connection {}
Tips:Client與BinaryClient的區別:BinaryClient是原生客戶端,而Client封裝性更好,也被叫做高級客戶端。
Connection類中主要是socket進行通信,一個Connection已經就是一個最基礎的客戶端。
另外增加各種協議層次的發送命令和收取結果的方法,都是通過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;
……
}
主要方法:
1、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);
}
}
}
2、disconnect()
public void disconnect() {
if (isConnected()) {
try {
outputStream.flush();
socket.close();
} catch (IOException ex) {
broken = true;
throw new JedisConnectionException(ex);
} finally {
IOUtils.closeQuietly(socket);
}
}
}
3、isConnected()
public boolean isConnected() {
return socket != null && socket.isBound() && !socket.isClosed() && socket.isConnected()
&& !socket.isInputShutdown() && !socket.isOutputShutdown();
}