jedis 连接池的使用

简单的使用连接池

// redis连接池的配置首先要创建一个连接池配置对象
		JedisPoolConfig config = new JedisPoolConfig();

		// 创建Jedis连接池对象
		JedisPool jedisPool = new JedisPool(config, "localhost", 6379);

		// 获取连接
		Jedis jedis = jedisPool.getResource();

		// 使用
		jedis.set("demo", "demo111");

		// 关闭,归还连接到连接池
		jedis.close();

模板方法的使用方式

如果在每个需要使用jedis的地方都需要获取jedisPool,然后获取连接,操作完成之后,还需要将连接使用,大量的重复的工作,如何避免大量的重复代码呢?

@FunctionalInterface
public interface JedisCallback<T> {
	public T invoke(Jedis jedis);
}

public class JedisOperation {

	private JedisPool jedisPool;

	public void init() {
		// 比较特殊的是,redis连接池的配置首先要创建一个连接池配置对象
		JedisPoolConfig config = new JedisPoolConfig();
		// 当然这里还有设置属性的代码
		// 创建Jedis连接池对象
		jedisPool = new JedisPool(config, "localhost", 6379);
	}

	public <T> T execute(JedisCallback<T> callback) {
		Jedis jedis = jedisPool.getResource();
		try {
			return callback.invoke(jedis);
		}
		catch (Exception ex) {
			throw new RuntimeException("");
		}
		finally {
			jedis.close();
		}
	}
}

在需要使用jedis的地方使用以下方式。

JedisOperation jedisOperation = new JedisOperation();
jedisOperation.init();
String demo = jedisOperation.execute(jedis -> jedis.get("demo"));

System.out.printf(demo);

JedisOperation 可以与spring整合,通过注入的方式使用。

动态代理的使用方式

虽然使用以上方式可以复用一些代码,但是还是需要在 jedisOperation.execute 等一大串代码,有没有更简单的方式呢?
可以通过动态代理的方式代理Jedis对象,在Jedis相关的操作前后获取链接和释放链接。
Jdk动态代理要通过接口的方式实现,需要找到Jedis的接口,通过代理该接口,达到操作Jedis的功能。Jedis实现的接口多,为了实现这么多接口的功能,可以定义一个接口,继承Jedis的接口,在通过代理改接口,达到调用Jedis的功能。

public interface IJedis extends JedisCommands, MultiKeyCommands {

}

public class JedisProxy<T> implements InvocationHandler {


	private JedisPool jedisPool;

	public void init() {
		// 比较特殊的是,redis连接池的配置首先要创建一个连接池配置对象
		JedisPoolConfig config = new JedisPoolConfig();
		// 当然这里还有设置属性的代码
		// 创建Jedis连接池对象
		jedisPool = new JedisPool(config, "localhost", 6379);
	}

	private Class<T> jedisInterface;

	public Class<T> getJedisInterface() {
		return jedisInterface;
	}

	public JedisProxy(Class<T> jedisInterface) {
		this.jedisInterface = jedisInterface;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Jedis jedis = jedisPool.getResource();

		try {
			return method.invoke(jedis, args);
		}

		catch (InvocationTargetException e) {
			throw new RuntimeException("出现异常");
		}
		catch (Exception e) {
			throw new RuntimeException("出现异常");
		}
		finally {
			jedis.close();
		}
	}
}
public class JedisProxyFactory<T> {

	private IJedis iJedis;


	public T newInstance(JedisProxy<T> jedisProxy) {
		return (T) Proxy.newProxyInstance(jedisProxy.getJedisInterface().getClassLoader(), new Class[] {jedisProxy.getJedisInterface()}, jedisProxy);
	}
}

在要使用的地方直接调用IJedis接口就好了,IJedis继承了Jedis的实现接口。可像使用Jedis方式一样使用IJedis。

JedisProxy<IJedis> jedisProxy = new JedisProxy<>(IJedis.class);
jedisProxy.init();

JedisProxyFactory jedisProxyFactory = new JedisProxyFactory();
IJedis jedis = (IJedis) jedisProxyFactory.newInstance(jedisProxy);
String demo = jedis.get("demo");

System.out.println(demo);

IJedis 可与spring整合,通过注入的方式使用

原文地址:https://www.cnblogs.com/nele/p/15555235.html