从第一章源码环境搭建中,Jedis 工具包连接 redis 客户端有三个,这一章主要讲 jedis 类连接 redis。
1、首先看下 jedis 类的继承关系图,可以看出 jedis 功能上以实现各种 command 和继承 BinaryJedis ,并没有很复杂的体系。
2、从名字可以就可以看出来,BinaryJedis 其实就是二进制,实现的方法都是二进制的。
API 文档:BinaryJedis (Jedis 2.1.0 API)
先看看 BinaryJedis 实现了的接口类有:
BasicCommands | 定义了redis基础命令,以 ping、DB相关等命令。 |
BinaryJedisCommands | 定义操作 key 的命令方法 |
MultiKeyBinaryCommands | 定义集合、队列的命令方法 |
AdvancedBinaryJedisCommands | 定义了配置和日志的命令对应方法 |
BinaryScriptingCommands | 定义了执行脚本命令的方法 |
Closeable | 定义关闭方法 |
从这些就可以看到 BinaryJedis 其实就是实现所有 redis 的命令,以二进制的方式实现,下面先看下构造方法:
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") || uri.getScheme().equals("rediss"))) {
initializeClientFromURI(uri);
} else {
client = new Client(host);
}
}
public BinaryJedis(final String host, final int port) {
client = new Client(host, port);
}
public BinaryJedis(final String host, final int port, final boolean ssl) {
client = new Client(host, port, ssl);
}
public BinaryJedis(final String host, final int port, final boolean ssl,
final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
}
public BinaryJedis(final String host, final int port, final int timeout) {
client = new Client(host, port);
client.setConnectionTimeout(timeout);
client.setSoTimeout(timeout);
}
public BinaryJedis(final String host, final int port, final int timeout, final boolean ssl) {
client = new Client(host, port, ssl);
client.setConnectionTimeout(timeout);
client.setSoTimeout(timeout);
}
public BinaryJedis(final String host, final int port, final int timeout, final boolean ssl,
final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
client.setConnectionTimeout(timeout);
client.setSoTimeout(timeout);
}
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 String host, final int port, final int connectionTimeout,
final int soTimeout, final boolean ssl) {
client = new Client(host, port, ssl);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
}
public BinaryJedis(final String host, final int port, final int connectionTimeout,
final int soTimeout, final boolean ssl, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
}
public BinaryJedis(final JedisShardInfo shardInfo) {
client = new Client(shardInfo.getHost(), shardInfo.getPort(), shardInfo.getSsl(),
shardInfo.getSslSocketFactory(), shardInfo.getSslParameters(),
shardInfo.getHostnameVerifier());
client.setConnectionTimeout(shardInfo.getConnectionTimeout());
client.setSoTimeout(shardInfo.getSoTimeout());
client.setPassword(shardInfo.getPassword());
client.setDb(shardInfo.getDb());
}
public BinaryJedis(URI uri) {
initializeClientFromURI(uri);
}
public BinaryJedis(URI uri, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier);
}
public BinaryJedis(final URI uri, final int timeout) {
initializeClientFromURI(uri);
client.setConnectionTimeout(timeout);
client.setSoTimeout(timeout);
}
public BinaryJedis(final URI uri, final int timeout, final SSLSocketFactory sslSocketFactory,
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier);
client.setConnectionTimeout(timeout);
client.setSoTimeout(timeout);
}
public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout) {
initializeClientFromURI(uri);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
}
public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout,
final SSLSocketFactory sslSocketFactory,final SSLParameters sslParameters,
final HostnameVerifier hostnameVerifier) {
initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier);
client.setConnectionTimeout(connectionTimeout);
client.setSoTimeout(soTimeout);
}
其实这个构造方法,就是redis的一些信息,并没什么特殊的。这里会看到一个 Client 的类,其实 BinaryJedis 的方法实现都是调用 Client 的方法。
下面看下 BinaryJedis 的 get 方法:
@Override
public byte[] get(final byte[] key) {
checkIsInMultiOrPipeline();
client.get(key);
return client.getBinaryBulkReply();
}
// 如果client正在执行事务或者管道流操作,其它命令无法执行
protected void checkIsInMultiOrPipeline() {
if (client.isInMulti()) {
throw new JedisDataException(
"Cannot use Jedis when in Multi. Please use Transaction or reset jedis state.");
} else if (pipeline != null && pipeline.hasPipelinedResponse()) {
throw new JedisDataException(
"Cannot use Jedis when in Pipeline. Please use Pipeline or reset jedis state .");
}
}
通过这个方法其实就可以知道了,BinaryJedis 本身并不具体实现,都是使用的是 client 实现的方法,最后获取返回的二进制字节直接返回。