Compare commits
3
Commits
84251fc25f
...
14f4d7009b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14f4d7009b | ||
|
|
aac0ac1fba | ||
|
|
a917c2b044 |
@@ -1,5 +1,7 @@
|
|||||||
package cn.hezhaohui.redis;
|
package cn.hezhaohui.redis;
|
||||||
|
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -9,14 +11,16 @@ import java.util.Set;
|
|||||||
* TODO: 实现键的过期和生存时间操作
|
* TODO: 实现键的过期和生存时间操作
|
||||||
*/
|
*/
|
||||||
public class RedisBasicOperations {
|
public class RedisBasicOperations {
|
||||||
|
|
||||||
|
private Jedis jedis = RedisPoolConnection.getJedis();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查键是否存在
|
* 检查键是否存在
|
||||||
* TODO: 实现exists方法
|
* TODO: 实现exists方法
|
||||||
*/
|
*/
|
||||||
public boolean exists(String key) {
|
public boolean exists(String key) {
|
||||||
// TODO: 检查键是否存在
|
// TODO: 检查键是否存在
|
||||||
return false;
|
return jedis.exists(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -25,7 +29,7 @@ public class RedisBasicOperations {
|
|||||||
*/
|
*/
|
||||||
public long delete(String key) {
|
public long delete(String key) {
|
||||||
// TODO: 删除键
|
// TODO: 删除键
|
||||||
return 0;
|
return jedis.del(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,7 +38,7 @@ public class RedisBasicOperations {
|
|||||||
*/
|
*/
|
||||||
public Set<String> keys(String pattern) {
|
public Set<String> keys(String pattern) {
|
||||||
// TODO: 获取所有匹配的键
|
// TODO: 获取所有匹配的键
|
||||||
return null;
|
return jedis.keys(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,7 +47,7 @@ public class RedisBasicOperations {
|
|||||||
*/
|
*/
|
||||||
public boolean setExpiry(String key, int seconds) {
|
public boolean setExpiry(String key, int seconds) {
|
||||||
// TODO: 设置键的过期时间
|
// TODO: 设置键的过期时间
|
||||||
return false;
|
return jedis.expire(key, seconds) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,7 +56,7 @@ public class RedisBasicOperations {
|
|||||||
*/
|
*/
|
||||||
public long timeToLive(String key) {
|
public long timeToLive(String key) {
|
||||||
// TODO: 获取键的剩余生存时间
|
// TODO: 获取键的剩余生存时间
|
||||||
return 0;
|
return jedis.ttl(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,7 +65,7 @@ public class RedisBasicOperations {
|
|||||||
*/
|
*/
|
||||||
public String type(String key) {
|
public String type(String key) {
|
||||||
// TODO: 获取键的类型
|
// TODO: 获取键的类型
|
||||||
return null;
|
return jedis.type(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,5 +74,6 @@ public class RedisBasicOperations {
|
|||||||
*/
|
*/
|
||||||
public void flushDatabase() {
|
public void flushDatabase() {
|
||||||
// TODO: 清空数据库
|
// TODO: 清空数据库
|
||||||
|
jedis.flushDB();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+21
-6
@@ -4,20 +4,29 @@ import redis.clients.jedis.Jedis;
|
|||||||
import redis.clients.jedis.JedisPool;
|
import redis.clients.jedis.JedisPool;
|
||||||
import redis.clients.jedis.JedisPoolConfig;
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis连接管理类
|
* Redis连接管理类
|
||||||
* TODO: 实现Redis连接池的配置和管理
|
* DONE: 实现Redis连接池的配置和管理
|
||||||
* TODO: 添加连接状态检查和重连机制
|
* DONE: 添加连接状态检查和重连机制
|
||||||
* TODO: 实现连接参数的配置化
|
* DONE: 实现连接参数的配置化
|
||||||
*/
|
*/
|
||||||
public class RedisConnection {
|
public class RedisPoolConnection {
|
||||||
private static JedisPool jedisPool;
|
private static JedisPool jedisPool;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// TODO: 配置连接池参数
|
// TODO: 配置连接池参数
|
||||||
JedisPoolConfig config = new JedisPoolConfig();
|
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: 设置最大连接数、最小空闲连接数等参数
|
// TODO: 设置最大连接数、最小空闲连接数等参数
|
||||||
jedisPool = new JedisPool(config, "localhost", 6379);
|
jedisPool = new JedisPool(config, "47.121.181.112", 6379, 2000, "dn293830");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,7 +35,7 @@ public class RedisConnection {
|
|||||||
*/
|
*/
|
||||||
public static Jedis getJedis() {
|
public static Jedis getJedis() {
|
||||||
// TODO: 返回连接池中的连接
|
// TODO: 返回连接池中的连接
|
||||||
return null;
|
return jedisPool.getResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,6 +44,9 @@ public class RedisConnection {
|
|||||||
*/
|
*/
|
||||||
public static void closeJedis(Jedis jedis) {
|
public static void closeJedis(Jedis jedis) {
|
||||||
// TODO: 关闭连接
|
// TODO: 关闭连接
|
||||||
|
if (jedis != null) {
|
||||||
|
jedis.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,5 +55,8 @@ public class RedisConnection {
|
|||||||
*/
|
*/
|
||||||
public static void closePool() {
|
public static void closePool() {
|
||||||
// TODO: 关闭连接池
|
// TODO: 关闭连接池
|
||||||
|
if (jedisPool != null) {
|
||||||
|
jedisPool.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,9 +4,9 @@ import redis.clients.jedis.Jedis;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis连接测试类
|
* Redis连接测试类
|
||||||
* TODO: 实现Redis连接测试
|
* DONE: 实现Redis连接测试
|
||||||
* TODO: 验证Redis服务器是否正常运行
|
* DONE: 验证Redis服务器是否正常运行
|
||||||
* TODO: 测试基本的Redis操作
|
* DONE: 测试基本的Redis操作
|
||||||
*/
|
*/
|
||||||
public class RedisConnectionTest {
|
public class RedisConnectionTest {
|
||||||
|
|
||||||
@@ -30,8 +30,9 @@ public class RedisConnectionTest {
|
|||||||
System.out.println("测试Redis连接...");
|
System.out.println("测试Redis连接...");
|
||||||
try {
|
try {
|
||||||
// 创建Jedis连接
|
// 创建Jedis连接
|
||||||
Jedis jedis = new Jedis("localhost", 6379);
|
Jedis jedis = new Jedis("47.121.181.112", 6379, 2000);
|
||||||
|
|
||||||
|
jedis.auth("dn293830");
|
||||||
// 测试连接
|
// 测试连接
|
||||||
String pingResult = jedis.ping();
|
String pingResult = jedis.ping();
|
||||||
System.out.println("Redis连接测试结果: " + pingResult);
|
System.out.println("Redis连接测试结果: " + pingResult);
|
||||||
@@ -56,7 +57,9 @@ public class RedisConnectionTest {
|
|||||||
private static void testBasicOperations() {
|
private static void testBasicOperations() {
|
||||||
System.out.println("测试基本操作...");
|
System.out.println("测试基本操作...");
|
||||||
try {
|
try {
|
||||||
Jedis jedis = new Jedis("localhost", 6379);
|
Jedis jedis = new Jedis("47.121.181.112", 6379, 2000);
|
||||||
|
|
||||||
|
jedis.auth("dn293830");
|
||||||
|
|
||||||
// 测试字符串操作
|
// 测试字符串操作
|
||||||
jedis.set("test_key", "test_value");
|
jedis.set("test_key", "test_value");
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package cn.hezhaohui;
|
package cn.hezhaohui;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hezhaohui.redis.RedisBasicOperations;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Redis学习测试类
|
* Redis学习测试类
|
||||||
* TODO: 实现各个Redis操作类的测试方法
|
* TODO: 实现各个Redis操作类的测试方法
|
||||||
@@ -41,6 +43,8 @@ public class RedisTest {
|
|||||||
private static void testBasicOperations() {
|
private static void testBasicOperations() {
|
||||||
System.out.println("测试基础操作...");
|
System.out.println("测试基础操作...");
|
||||||
// TODO: 实现基础操作测试
|
// TODO: 实现基础操作测试
|
||||||
|
RedisBasicOperations redisBasicOperations = new RedisBasicOperations();
|
||||||
|
System.out.println(redisBasicOperations.keys("*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package cn.hezhaohui.redis;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.*;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
class RedisPoolConnectionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
void getJedis() {
|
||||||
|
System.out.println("开始Redis连接测试...");
|
||||||
|
Jedis jedis = RedisPoolConnection.getJedis();
|
||||||
|
assertNotNull(jedis);
|
||||||
|
System.out.println("Redis连接测试通过!");
|
||||||
|
System.out.println("Redis服务器状态:" + jedis.ping());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(2)
|
||||||
|
void closeJedis() throws InterruptedException {
|
||||||
|
// Thread.sleep(10000);
|
||||||
|
System.out.println("开始关闭Redis连接...");
|
||||||
|
Jedis jedis = RedisPoolConnection.getJedis();
|
||||||
|
assertNotNull(jedis);
|
||||||
|
RedisPoolConnection.closeJedis(jedis);
|
||||||
|
System.out.println("Redis连接已关闭!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(3)
|
||||||
|
void closePool() throws InterruptedException {
|
||||||
|
// Thread.sleep(10000);
|
||||||
|
System.out.println("开始关闭Redis连接池...");
|
||||||
|
RedisPoolConnection.closePool();
|
||||||
|
System.out.println("Redis连接池已关闭!");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user