Jedis笔记
Jedis
我们要使用java来操作Redis
jedsi是redsi官方推荐工具!使用java操作redsi操作中间件!如果你要使用java操作redis,那么一定要对jedis了解
测试
1.导入jar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
2.编码测试
- 连接数据库
- 操作命令
- 断开连接
常用的api
String
List
Set
Hash
Zset
所有的api命令,就是我们对应的上面学习指令,一个都没有变化
远程连接redis
1.在阿里云下开启6379端口号
2.linux下查看防火墙
firewall-cmd --list-ports
可以看到没有6379
20/tcp 21/tcp 22/tcp 80/tcp 8888/tcp 39000-40000/tcp 8080/tcp 8080/udp 3306/tcp 3306/udp
3.开启端口号
firewall-cmd --zone=public --add-port=6379/tcp --permanent
4.重启
firewall-cmd --reload
基本操作
//1.new一个Jedis对象
Jedis jedis = new Jedis("121.40.162.47",6379);
//redis密码没有密码的话则
jedis.auth("123456");
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","word");
jsonObject.put("name","tang");
//开启事务
Transaction multi = jedis.multi();
String string = jsonObject.toJSONString();
try {
multi.set("user1",string);
multi.set("user2",string);
multi.exec();
}catch (Exception e){
multi.discard();
e.printStackTrace();
}finally {
//关闭连接
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
jedis.close();
}