Jedis
# Jedis
可以操作 Redis 的 Client 有许多,参考文档。具体使用参考 Jedis 的 Github 仓库。
使用时需要添加依赖,具体看文档!
# 简单使用步骤
//1. 获取连接
Jedis jedis = new Jedis("localhost",6379);
//2. 操作
jedis.set("username","zhangsan");
//3. 关闭连接
jedis.close();
1
2
3
4
5
6
2
3
4
5
6
# 工具类封装
redis.properties
host=111.51.216.173
port=6379
maxTotal=30
maxIdle=10
1
2
3
4
2
3
4
public class JedisPoolUtils {
private static final JedisPool jedisPool;
private static final int maxTotal;
private static final int maxIdle;
private static final String host;
private static final int port;
static {
ResourceBundle redisBundle = ResourceBundle.getBundle("redis");
maxTotal = Integer.parseInt(redisBundle.getString("maxTotal"));
maxIdle = Integer.parseInt(redisBundle.getString("maxIdle"));
host = redisBundle.getString("host");
port = Integer.parseInt(redisBundle.getString("port"));
//获取数据,设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
//初始化JedisPool
jedisPool = new JedisPool(config, host, port);
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
上次更新: 2021/06/21, 15:45:42