Skip to content

Commit c916ed1

Browse files
committed
redis 连接池
0 parents  commit c916ed1

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

jedis/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>redis</artifactId>
7+
<groupId>org.clxmm</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>jedis</artifactId>
13+
14+
15+
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>redis.clients</groupId>
20+
<artifactId>jedis</artifactId>
21+
<version>3.2.0</version>
22+
<type>jar</type>
23+
<scope>compile</scope>
24+
</dependency>
25+
26+
</dependencies>
27+
28+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.clxmm.jedis;
2+
3+
import redis.clients.jedis.Jedis;
4+
5+
/**
6+
* @author clx
7+
* @date 2020-06-13 20:09
8+
*/
9+
public interface CallWithJedis {
10+
11+
void call(Jedis jedis);
12+
13+
14+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.clxmm.jedis;
2+
3+
import redis.clients.jedis.Jedis;
4+
import redis.clients.jedis.JedisPool;
5+
6+
/**
7+
* @author clx
8+
* @date 2020-06-13 15:40
9+
*/
10+
public class JedisPoolTest {
11+
12+
13+
/* public static void main(String[] args) {
14+
15+
//1. 构造一个 Jedis 连接池
16+
JedisPool pool = new JedisPool("127.0.0.1", 6379);
17+
//2. 从连接池中获取一个 Jedis 连接
18+
Jedis jedis = pool.getResource();
19+
jedis.auth("123456");
20+
21+
try {
22+
//3 .jedis 操作
23+
String ping = jedis.ping();
24+
System.out.println(ping);
25+
26+
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
} finally {
30+
if (jedis != null) {
31+
//4. 归还连接
32+
jedis.close();
33+
}
34+
}
35+
36+
}*/
37+
38+
39+
public static void main(String[] args) {
40+
Redis redis = new Redis();
41+
redis.execute(jedis -> {
42+
jedis.auth("123456");
43+
String ping = jedis.ping();
44+
System.out.println(ping);
45+
});
46+
47+
}
48+
49+
public static void main1(String[] args) {
50+
51+
//1. 构造一个 Jedis 连接池
52+
JedisPool pool = new JedisPool("127.0.0.1", 6379);
53+
//2. 从连接池中获取一个 Jedis 连接
54+
55+
try (Jedis jedis = pool.getResource()) {
56+
jedis.auth("123456");
57+
String ping = jedis.ping();
58+
System.out.println(ping);
59+
}
60+
61+
}
62+
63+
64+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.clxmm.jedis;
2+
3+
import redis.clients.jedis.Jedis;
4+
5+
/**
6+
* @author clx
7+
* @date 2020-06-13 15:34
8+
*/
9+
public class MyJedis {
10+
11+
12+
public static void main(String[] args) {
13+
14+
// 构造一个jedis 对象,默认端口不用配置
15+
Jedis jedis = new Jedis("127.0.0.1", 6379);
16+
//2. 密码验证
17+
jedis.auth("123456");
18+
String ping = jedis.ping();
19+
//4.返回 pong 表示连接成功
20+
System.out.println(ping);
21+
}
22+
23+
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.clxmm.jedis;
2+
3+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
4+
import redis.clients.jedis.Jedis;
5+
import redis.clients.jedis.JedisPool;
6+
7+
/**
8+
* @author clx
9+
* @date 2020-06-13 20:10
10+
*/
11+
public class Redis {
12+
private JedisPool pool;
13+
14+
public Redis() {
15+
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
16+
//连接池最大空闲数
17+
config.setMaxIdle(300);
18+
//最大连接数·
19+
config.setMaxTotal(1_000);
20+
//最大等待时间 -1 表示没有限制
21+
config.setMaxWaitMillis(30_000);
22+
//空闲时间检查有效性
23+
config.setTestOnBorrow(true);
24+
25+
pool = new JedisPool(config, "127.0.0.1", 6379, 30_000, "123456");
26+
}
27+
28+
public void execute(CallWithJedis withJedis) {
29+
try (Jedis jedis = pool.getResource()) {
30+
withJedis.call(jedis);
31+
}
32+
}
33+
34+
35+
}

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.clxmm</groupId>
8+
<artifactId>redis</artifactId>
9+
<packaging>pom</packaging>
10+
<version>1.0-SNAPSHOT</version>
11+
<modules>
12+
<module>jedis</module>
13+
</modules>
14+
15+
16+
</project>

0 commit comments

Comments
 (0)