Jedis
# Jedis
Java 连接 redis 服务 Jedis、SpringData Redis、Lettuce
- 导入 jar 包
//获取连接
Jedis jedis =new Jedis("192.168.130.128",6379);
jedis.auth("123456");
//执行
jedis.set("name2","123");
System.out.println(jedis.get("name"));
//关闭连接
jedis.close();
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 连接池
导入 jedis.jar commons-pool.jar
private static int MaxTotal, maxIdel, port;
private static String host;
private static JedisPoolConfig jpc;
private static JedisPool jp;
static {
ResourceBundle bundle = ResourceBundle.getBundle("笔记/redis/src/redis");
MaxTotal = Integer.parseInt(bundle.getString("redis.maxTotal"));
maxIdel = Integer.parseInt(bundle.getString("redis.maxIdel"));
host = bundle.getString("redis.host");
port = Integer.parseInt(bundle.getString("redis.port"));
jpc = new JedisPoolConfig();
jpc.setMaxTotal(MaxTotal);
jpc.setMaxIdle(maxIdel);
jp = new JedisPool(jpc, host, port, 2000, "123456");
}
public static Jedis getJedis() {
return jp.getResource();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
创建 redis.properties
redis.maxTotal=50
redis.maxIdel=10
redis.host=192.168.130.128
redis.port=6379
1
2
3
4
2
3
4
# 持久化

# RDB
- save 保存
dbfilename "dump-6379.rdb" #rdb快照保存的文件名 不设置也有默认名当save执行时
rdbcopression yes #存储到本地是否压缩 默认为yes no则不压缩
rdbchecksum yes #设置读写文件过程是否进行RDB格式校验 默认为yes
1
2
3
2
3
save 指令会阻塞当前服务器直到 RDB 完成 线上不建议使用
- bgsave 后台保存

- save second changes 设置自动持久化 满足限定时间内 key 的变化数据则进行 save
#在redis配置文件中设置
save second changes #设置自动持久化 如:save 10 2 10秒中2个key发生变化 自动保存
1
2
2


- debug reload 服务器运行过程中重启 也会执行一次 RDB
- shutdown save 关闭服务器并 save
# AOF
配置文件
appendonly yes #开启AOF持久化 默认为no不开启
appendfilename appendonly-6379.aof # 保存文件名 有默认名
appendfsync always|everysec|no #AOF写数据策略 默认为everysec
#always 每次写入操作同步到AOF中
#everysec 每秒将缓冲区的同步到AOF
#no 系统控制 整个过程不可控
1
2
3
4
5
6
2
3
4
5
6
RDB 和 AOF 都是影响了数据库的数据才作记录
- bgrewriteaof 后台重写 aof 将 aof 文件进行压缩简化合并


编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48