62 lines
1.6 KiB
Java
62 lines
1.6 KiB
Java
package cn.hezhaohui.redis;
|
|
|
|
import redis.clients.jedis.Jedis;
|
|
import redis.clients.jedis.JedisPool;
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
import java.time.Duration;
|
|
|
|
/**
|
|
* Redis连接管理类
|
|
* DONE: 实现Redis连接池的配置和管理
|
|
* DONE: 添加连接状态检查和重连机制
|
|
* DONE: 实现连接参数的配置化
|
|
*/
|
|
public class RedisPoolConnection {
|
|
private static JedisPool jedisPool;
|
|
|
|
static {
|
|
// TODO: 配置连接池参数
|
|
JedisPoolConfig config = new JedisPoolConfig();
|
|
config.setMaxTotal(20);
|
|
config.setMaxIdle(10);
|
|
config.setMinIdle(5);
|
|
config.setMaxWait(Duration.ofMillis(1000));
|
|
config.setTestOnBorrow(true);
|
|
config.setTestWhileIdle(true);
|
|
config.setBlockWhenExhausted(true);
|
|
// TODO: 设置最大连接数、最小空闲连接数等参数
|
|
jedisPool = new JedisPool(config, "47.121.181.112", 6379, 2000, "dn293830");
|
|
}
|
|
|
|
/**
|
|
* 获取Redis连接
|
|
* TODO: 实现连接获取逻辑
|
|
*/
|
|
public static Jedis getJedis() {
|
|
// TODO: 返回连接池中的连接
|
|
return jedisPool.getResource();
|
|
}
|
|
|
|
/**
|
|
* 关闭连接
|
|
* TODO: 实现连接关闭逻辑
|
|
*/
|
|
public static void closeJedis(Jedis jedis) {
|
|
// TODO: 关闭连接
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 关闭连接池
|
|
* TODO: 实现连接池关闭逻辑
|
|
*/
|
|
public static void closePool() {
|
|
// TODO: 关闭连接池
|
|
if (jedisPool != null) {
|
|
jedisPool.close();
|
|
}
|
|
}
|
|
} |