Files
redis-template/src/main/java/cn/hezhaohui/redis/RedisPoolConnection.java
T

62 lines
1.6 KiB
Java
Raw Normal View History

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