jedis是Redis官网推荐的java客户端实现。下面分析一下JedisPool的实现
JedisPool源码如下:
package redis.clients.jedis;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import redis.clients.util.Pool;
public class JedisPool extends Pool<Jedis>
{
public JedisPool(GenericObjectPool.Config poolConfig, String host)
{
this(poolConfig, host, 6379, 2000, null);
}
public JedisPool(String host, int port)
{
super(new GenericObjectPool.Config(), new JedisFactory(host, port, 2000, null));
}
public JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password)
{
super(poolConfig, new JedisFactory(host, port, timeout, password));
}
public JedisPool(GenericObjectPool.Config poolConfig, String host, int port)
{
this(poolConfig, host, port, 2000, null);
}
public JedisPool(GenericObjectPool.Config poolConfig, String host, int port, int timeout)
{
this(poolConfig, host, port, timeout, null);
}
}
JedisPool继承了Pool,Pool的源码如下:
package redis.clients.util;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
public abstract class Pool<T>
{
private final GenericObjectPool internalPool;
public Pool(GenericObjectPool.Config poolConfig, PoolableObjectFactory factory)
{
this.internalPool = new GenericObjectPool(factory, poolConfig);
}
public T getResource()
{
try {
return this.internalPool.borrowObject();
} catch (Exception e) {
throw new JedisConnectionException("Could not get a resource from the pool", e);
}
}
public void returnResource(T resource)
{
try {
this.internalPool.returnObject(resource);
} catch (Exception e) {
throw new JedisException("Could not return the resource to the pool", e);
}
}
public void returnBrokenResource(T resource)
{
try {
this.internalPool.invalidateObject(resource);
} catch (Exception e) {
throw new JedisException("Could not return the resource to the pool", e);
}
}
public void destroy()
{
try {
this.internalPool.close();
} catch (Exception e) {
throw new JedisException("Could not destroy the pool", e);
}
}
}
至此我们已经清楚了,JedisPool使用了apache的GenericObjectPool来作为redis连接管理pool。GenericObjectPool的官方地址是:http://commons.apache.org/pool/
JedisFactory是PoolableObjectFactory的子类,PoolableObjectFactory提供了可以被Pool管理的对象的若干生命周期方法,JedisFactory的源码如下:
package redis.clients.jedis;
import org.apache.commons.pool.BasePoolableObjectFactory;
class JedisPool$JedisFactory extends BasePoolableObjectFactory
{
private final String host;
private final int port;
private final int timeout;
private final String password;
public JedisPool$JedisFactory(String host, int port, int timeout, String password)
{
this.host = host;
this.port = port;
this.timeout = ((timeout > 0) ? timeout : -1);
this.password = password;
}
public Object makeObject()
throws Exception
{
Jedis jedis;
if (this.timeout > 0)
jedis = new Jedis(this.host, this.port, this.timeout);
else {
jedis = new Jedis(this.host, this.port);
}
jedis.connect();
if (null != this.password) {
jedis.auth(this.password);
}
return jedis;
}
public void destroyObject(Object obj) throws Exception {
if (obj instanceof Jedis) {
Jedis jedis = (Jedis)obj;
if (!(jedis.isConnected())) return;
try {
try {
jedis.quit();
} catch (Exception e) {
}
jedis.disconnect();
}
catch (Exception e)
{
}
}
}
public boolean validateObject(Object obj) {
if (obj instanceof Jedis) {
Jedis jedis = (Jedis)obj;
try {
return ((jedis.isConnected()) && (jedis.ping().equals("PONG")));
} catch (Exception e) {
return false;
}
}
return false;
}
}
上面代码非常清楚地说明了JedisFactory如何构建、销毁、验证Jedis对象。这些生命周期方法会被
GenericObjectPool 的borrowObject,returnObject,invalidateObject等方法调用。
分享到:
相关推荐
4. **Pooling** - 为了有效地管理连接资源,Jedis使用了连接池(`JedisPool`和`GenericObjectPoolConfig`)。连接池可以减少创建和销毁连接的开销,提高系统效率。 5. **Exception Handling** - Jedis封装了与Redis...
`jedis.zip`包含的工具类是为了简化Java程序员与Redis服务器之间的交互,使得操作Redis更加便捷。下面我们将详细探讨这两个关键的类:`JedisTemplate`和`JedisProvider`。 `JedisTemplate`是核心的模板类,它封装了...
分析了redis的: 1)java客户端实现源码 2)分析了连接channel,包括命令时客户端和服务端的socket连接;...3)分析了JedisPool连接池配置 4) 分析了命令get set hmset 等逻辑 5)分析了subscribe实现源码
在源码中,你可以看到`JedisPool`和`JedisPoolConfig`类,它们负责资源的管理和配置。使用`Jedis`对象执行命令,如`set`、`get`、`hset`、`lpush`等,以操作Redis中的键值对、哈希、列表等数据结构。 2. **连接与...
JedisPool jedisPool = new JedisPool(config, "localhost", 6379); try (Jedis jedis = jedisPool.getResource()) { jedis.set("key", "value"); String value = jedis.get("key"); System.out.println(...
linux中安装redis和jedis及jedispool; redis安装所遇到的问题;
jedis 2.5.2 源码
1. **连接管理**:Jedis提供了连接池功能,通过`JedisPool`和`JedisPoolConfig`类来管理和配置连接池。连接池可以有效地复用连接,减少创建和销毁连接的开销,提高系统性能。 2. **命令执行**:Jedis封装了大量的...
JedisPool类实现了连接池的创建、获取和归还连接的逻辑,确保了资源的有效利用。 三、命令执行机制 Jedis通过CommandExecutor接口实现命令的发送和接收。命令通过CommandProtocol类转化为二进制数据,然后通过...
JedisPool是Redis客户端Jedis的一个关键组件,它提供了一种池化的机制来管理多个Jedis实例,从而提高性能和资源利用效率。在配置JedisPool时,了解其参数的意义至关重要,以确保正确地调整和优化连接池的行为。以下...
JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379); ``` 4. 从连接池获取Jedis实例进行操作: ```java Jedis jedis = jedisPool.getResource(); try { jedis.set("key", "value"); ...
`配置连接池参数,然后`JedisPool pool = new JedisPool(config, "localhost", 6379);`创建连接池,`Jedis jedis = pool.getResource();`获取连接,操作完成后使用`jedis.close();`归还连接。 ### Jedis与Redis其他...
**三、Jedis 2.9.0的源码分析** 1. **设计模式**:Jedis的源码中应用了多种设计模式,如工厂模式(用于创建Jedis实例)、单例模式(用于连接池管理)和装饰器模式(用于添加额外功能,如连接池的Jedis对象)。 2. ...
jedispool连redis高并发卡死的问题 jedispool是一种高性能的redis客户端连接池,它可以帮助开发者快速的连接redis服务器,并提供了许多高级特性来提高性能和可靠性。然而,在高并发的情况下,jedispool可能会出现...
JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379); ``` 4. **执行命令**: 通过JedisPool获取连接,执行Redis命令,然后返回连接到池中。 ```java Jedis resource = jedisPool.getResource...
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisClient { private static JedisPool pool; static { JedisPoolConfig config = new JedisPoolConfig()...
3. 初始化`JedisPool`:使用`JedisPoolConfig`创建一个`JedisPool`实例,指定Redis服务器的地址、端口、密码等信息。 4. 从`JedisPool`中获取`Jedis`实例:在需要操作Redis时,从池中借用一个`Jedis`对象。 5. 使用...
jedisPool = new JedisPool(poolConfig, "localhost", 6379); } public static void main(String[] args) { try (Jedis jedis = jedisPool.getResource()) { jedis.set("key", "value"); String value = ...
JedisPool jedisPool = new JedisPool(config, "localhost", 6379); ``` 3. 从池中获取并使用Jedis实例: ```java Jedis jedis = jedisPool.getResource(); String result = jedis.get("key"); ``` 4. 归还...
jedisPool = new JedisPool(poolConfig, "localhost", 6379); } public static Jedis getConnection() { return jedisPool.getResource(); } public static void releaseConnection(Jedis jedis) { if ...