之前几篇文章讲的都是在 Linux 环境下如何配置 redis,但是对于开发人员来讲,最终还是要通过Java 语言来连接并操作 redis 数据库,Jedis 是 redis 官方推荐的一个使用 Java 语言来连接 redis 的库
目录
1. 添加 Jedis 的 maven 依赖
2. 连接单机版 redis
3. 操作List,Hash,Set,Zset等类型
4. Jedis 连接池
5. 连接 redis 集群
6. Jedis 实现主从复制模式
1. 添加 Jedis 的 maven 依赖
<!-- 添加jedis依赖,操作redis-->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.3</version>
</dependency>
2. 连接单机版 redis
简单测试
public class JedisDemo {
public static void main(String[] args) {
// 通过 ip 和端口连接 redis
Jedis jedis = new Jedis("localhost",6379);
// test 服务器是否连接成功
System.out.println(jedis.ping());
// 如果设置密码,则需要输入密码
jedis.auth("123456");
// 设置数据
System.out.println(jedis.set("name","zhangsan"));
// 获取数据
System.out.println(jedis.get("name"));
// 判断是否存在某个 key
System.out.println(jedis.exists("name"));
// 删除 key
System.out.println(jedis.del("name"));
}
}
事务操作
public class JedisTransactionDemo {
public static void main(String[] args) {
// 通过 ip 和端口连接 redis
Jedis jedis = new Jedis("localhost",6379);
// watch 命令
jedis.watch("name");
// 开启事务
Transaction multi = jedis.multi();
multi.set("name","lisi");
multi.set("age","23");
// 撤销事务---撤销事务之后就删除了事务,在提交就会报错
multi.discard();
// 提交事务
multi.exec();
}
}
3. 操作List,Hash,Set,Zset等类型
public class JedisListDemo {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost",6379);
// 操作 List 类型
jedis.lpush("age","1","2","3","4","5","6","7","8","9");
jedis.rpush("user","张三","李四","王五");
System.out.println(jedis.lindex("age",2));
// 操作 Hash 类型
jedis.hset("person","person1","胡桃");
// 通过map方式传入value
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name","甘雨");
hashMap.put("age","23");
jedis.hset("genshin",hashMap);
System.out.println(jedis.hget("genshin","name"));
// 操作 Set 类型
jedis.sadd("names","a","b","c","a");
System.out.println(jedis.smembers("names"));
// 操作有序集合 Zset 类型
jedis.zadd("score",88,"张三");
jedis.zadd("score",90,"李四");
jedis.zadd("score",87,"王五");
System.out.println(jedis.zrange("score",0,1));
}
}
4. Jedis 连接池
每次客户端请求都新建或者断开连接比较耗费资源,与 mysql 一样,Jedis 也可以创建一个连接池,维护多个与 redis 服务器的 tcp 请求,每次客户端请求链接都从连接池中获得,使用完毕之后又释放到连接池中
public class JedisPoolDemo {
public static void main(String[] args) {
// 配置连接池
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10); // 设置最大连接数
config.setMaxIdle(5); // 设置最大空闲连接数
config.setBlockWhenExhausted(false); // 连接耗尽时不阻塞
// 创建连接池
JedisPool jedisPool = new JedisPool(config,"localhost",6379);
Jedis jedis = jedisPool.getResource();
jedis.set("name","王五");
System.out.println(jedis.get("name"));
// 释放连接池
jedisPool.close();
}
}
5. 连接 redis 集群
public class JedisClusterDemo {
public static void main(String[] args) {
// 配置连接池
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(20); // 设置最大连接数
config.setMaxIdle(20); // 设置最大空闲连接数
config.setBlockWhenExhausted(false); // 连接耗尽时不阻塞
// 构建集群节点
HashSet<HostAndPort> clusterNode = new HashSet<>();
// 三个主节点
clusterNode.add(new HostAndPort("localhost",6379));
clusterNode.add(new HostAndPort("localhost",6380));
clusterNode.add(new HostAndPort("localhost",6381));
// 三个从节点
clusterNode.add(new HostAndPort("localhost",26379));
clusterNode.add(new HostAndPort("localhost",26380));
clusterNode.add(new HostAndPort("localhost",26381));
JedisCluster jedisCluster = new JedisCluster(clusterNode,config);
jedisCluster.set("name","雷电将军");
jedisCluster.hset("person","name","王五");
jedisCluster.sadd("age","17","23","56");
System.out.println(jedisCluster.hget("person","name"));
// 关闭连接
jedisCluster.close();
}
}
6. Jedis 实现主从复制模式
public class JedisMasterSlaveDemo {
public static void main(String[] args) throws InterruptedException {
Jedis jedis_master = new Jedis("localhost",6379);
Jedis jedis_slave = new Jedis("localhost",26379);
// 配从不配主
jedis_slave.slaveof("localhost",6379);
// 主从复制模式: 默认主节点写,从节点读
jedis_master.set("dog", "hhh");
// 从节点更新需要时间
Thread.sleep(3000);
System.out.println(jedis_slave.get("dog"));
}
}