目录
- JedisAPI基本使用
- 1.导入相关依赖
- 2.连接成功测试
- 3.常用API
- 3.1 key相关操作
- 3.2 String
- 3.3 List
- 3.4 Set
- 3.5 Hash
- Jedis实现Redis事务
以下是Redis相关笔记总结,方便自己以后复习,同时也希望对大家有所帮助。
内容 | 地址链接 |
Redis在Linux环境下的详细安装教程 | |
Redis中五大基本数据类型和三种特殊数据类型 | |
Redis中基本事务操作及乐观锁的实现 | |
Java中使用JedisAPI操作Redis中五大基本数据类型 | |
Spring boot整合Redis(入门教程) | |
Redis主从复制详解(入门教程) | |
Spring boot整合Redis实现发布订阅(超详细) | |
Redis执行save命令时报错ERR |
JedisAPI基本使用
1.导入相关依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.4.2</version>
</dependency>
2.连接成功测试
本文ip相关信息隐去,在本地测试时将程序中*替换成ip即可
public class JedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***");
if (jedis != null){
jedis.auth("root");
System.out.println(jedis.ping());
}
}
}
3.常用API
3.1 key相关操作
public class JedisString {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***");
jedis.auth("root");
String s = jedis.flushDB();
System.out.println("清空数据(正确返回ok):" + s);
Boolean username = jedis.exists("username");
System.out.println("判断某个键是否存在(返回Boolen类型)" + username);
System.out.println("新增<'username','bbx'>的键值对:" + jedis.set("username", "bbx"));
System.out.println("新增<'password','password'>的键值对:" + jedis.set("password", "password"));
System.out.println("新增<'age','18'>的键值对:" + jedis.set("password", "18"));
Set<String> keys = jedis.keys("*");
System.out.println("所有的key:" + keys);
System.out.println("删除键password:" + jedis.del("password"));
System.out.println("判断键password是否存在:" + jedis.exists("password"));
System.out.println("查看键username所存储的值的类型:" + jedis.type("username"));
System.out.println("随机返回key空间的一个:" + jedis.randomKey());
System.out.println("重命名username的键,改为name:" + jedis.rename("username", "name"));
System.out.println("取出改后的name:" + jedis.get("name"));
System.out.println("按索引查询:" + jedis.select(1));
System.out.println("删除当前选择数据库中所有的key:" + jedis.flushDB());
System.out.println("返回当前选择数据库中所有的key的数目:" + jedis.dbSize());
System.out.println("删除当前所有数据库中所有的key:" + jedis.flushAll());
}
}
运行结果:
3.2 String
public class JedisTest {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***");
jedis.auth("root");
System.out.println(jedis.ping());
jedis.flushDB();
System.out.println("=============增加数据===============");
System.out.println(jedis.set("key1","value1"));
System.out.println(jedis.set("key2","value2"));
System.out.println(jedis.set("key3","value3"));
System.out.println("删除键key2:"+jedis.del("key2"));
System.out.println("获取建key2:"+jedis.get("key2"));
System.out.println("修改key1:"+jedis.set("key1","value1Changed"));
System.out.println("获取key1的值:"+jedis.get("key1"));
System.out.println("在key3后面加入值:"+jedis.append("key3","End"));
System.out.println("key3的值"+jedis.get("key3"));
System.out.println("=============批量增加数据===============");
System.out.println("增加多个键值对:"+jedis.mset("key01","value01","key02","value02","key03","value03","key04","value04"));
System.out.println("获取多个键值对:"+jedis.mget("key01","key02"));
System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03","key04"));
System.out.println("删除多个键值对:"+jedis.del("key01","key02"));
System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
jedis.flushDB();
System.out.println("=====================分布式锁,新增键值对防止覆盖原先值========================");
System.out.println(jedis.setnx("key1","value1"));
System.out.println(jedis.setnx("key2","value2"));
System.out.println(jedis.setnx("key2","value2-new"));
System.out.println(jedis.get("key1"));
System.out.println(jedis.get("key2"));
System.out.println("===========================新增键值对并设置有效时间=============================");
System.out.println(jedis.setex("key3",2,"value3"));
System.out.println(jedis.get("key3"));
try{
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(jedis.get("key3"));
System.out.println("===================获取原值,更新为新值========================");
System.out.println(jedis.getSet("key2","key2GetSet"));
System.out.println(jedis.get("key2"));
System.out.println("获取key2的值的字符串:"+jedis.getrange("key2",2,4));
}
}
运行结果:
3.3 List
public class JedisList {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***",6379);
jedis.auth("root");
jedis.flushDB();
System.out.println("=============添加一个List===============");
jedis.lpush("collections","ArrayList","Vector" ,"Stack","HashMap","WeakHashMap","LinkHasMap","HashMap");
jedis.lpush("collections","HashSet");
jedis.lpush("collections","TreeMap");
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println("collections区间0-3的元数;"+jedis.lrange("collections",0,3));
//删除列表指定的值,第二个参数为删除的个数(有重复时),后push进入的值先被删除,类似于出栈
System.out.println("删除指定元素个数:"+jedis.lrem("collections",2,"HashMap"));
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println("删除下标0-3区间之外的元素"+jedis.ltrim("collections",0,3));
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println("collections的列表出栈(左端):"+jedis.lpop("collections"));
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println("collections添加元素,从列表右端,与lpush相对应:"+jedis.rpush("collections","HashMap"));
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println("collections的列表出栈(右端):"+jedis.rpop("collections"));
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println(("修改collections指定下标1的内容:"+jedis.lset("collections",1,"LinkHashMap")));
System.out.println("collections的内容:"+jedis.lrange("collections",0,-1));
System.out.println("==================================================================");
System.out.println("collections的长度:"+jedis.llen("collections"));
System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections",2));
System.out.println("==================================================================");
jedis.lpush("sortedList","3","6","2","0","7","4");
System.out.println("sortedList排序前:"+jedis.lrange("sortedList",0,-1));
System.out.println(jedis.sort("sortedList"));
List<String> sortedList = jedis.sort("sortedList");
System.out.println("sortedList排序后:"+sortedList);
}
}
运行结果:
3.4 Set
public class JedisSet {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***",6379);
jedis.auth("root");
System.out.println("==================set测试,向集合中添加元素(不重复)=========================");
System.out.println(jedis.sadd("eleSet","e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println("第一次插入e6:"+jedis.sadd("eleSet","e6"));
System.out.println("第二次插入e6:"+jedis.sadd("eleSet","e6"));
System.out.println("eleSet的所有元素为"+jedis.smembers("eleSet"));
System.out.println("删除一个元素e0:"+jedis.srem("eleSet","e0"));
System.out.println("eleSet的所有元素为"+jedis.smembers("eleSet"));
System.out.println("删除两个元素e7,e6:"+jedis.srem("eleSet","e7","e6"));
System.out.println("eleSet的所有元素为"+jedis.smembers("eleSet"));
System.out.println("随机移除集合中的一个元素:"+jedis.spop("eleSet"));
System.out.println("随机移除集合中的一个元素:"+jedis.spop("eleSet"));
System.out.println("eleSet的所有元素为"+jedis.smembers("eleSet"));
System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet"));
System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet","e3"));
System.out.println("==========================================================================");
System.out.println(jedis.sadd("eleSet1","e1","e2","e4","e3","e0","e8","e7","e5"));
System.out.println(jedis.sadd("eleSet2","e1","e2","e4","e3","e0","e8"));
System.out.println("eleSet1中删除e1并存入eleSet3中;"+jedis.smove("eleSet1","eleSet3","e1"));
System.out.println("eleSet1中删除e2并存入eleSet3中;"+jedis.smove("eleSet1","eleSet3","e2"));
System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
System.out.println("eleSet3中的元素:"+jedis.smembers("eleSet3"));
System.out.println("=================================集合运算===================================");
System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
System.out.println("eleSet2中的元素:"+jedis.smembers("eleSet2"));
System.out.println("eleSet1和eleSet2的交集:"+jedis.sinter("eleSet1","eleSet2"));
System.out.println("eleSet1和eleSet2的并集:"+jedis.sunion("eleSet1","eleSet2"));
System.out.println("eleSet1和eleSet2的差集:"+jedis.sdiff("eleSet1","eleSet2"));
//求交集并将交集保存到dstkey的集合
jedis.sinterstore("eleSet4","eleSet1","eleSet2");
System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4"));
}
}
运行结果:
3.5 Hash
public class JedisHash {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***", 6379);
jedis.auth("root");
jedis.flushDB();
HashMap<String, String> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
//添加名称为hash(key)的hash元素
jedis.hmset("hash",map);
//向名称为hash的hash添加key为key5,value为value5的元素
jedis.hset("hash","key5","value5");
System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
System.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));
System.out.println("散列hash的所有值为:"+jedis.hvals("hash"));
}
}
运行结果:
Jedis实现Redis事务
事务相关的命令有:MULTI、EXEC、DISCARD、WATCH、UNWATCH。
public class JedisTX {
public static void main(String[] args) {
Jedis jedis = new Jedis("***.***.***.***");
jedis.auth("root");
//清楚当前数据库
jedis.flushDB();
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello","world");
jsonObject.put("name","bbx");
//开启事务
Transaction multi = jedis.multi();
String result = jsonObject.toJSONString();
//监控,乐观锁
// jedis.watch(result);
try {
multi.set("user1", result);
multi.set("user2", result);
//执行事务
multi.exec();
}catch (Exception e){
//如果事务执行失败就进行解锁
jedis.unwatch();
//放弃事务
multi.discard();
e.printStackTrace();
}finally {
System.out.println(jedis.get("user1"));
System.out.println(jedis.get("user2"));
//关闭连接
jedis.close();
}
}
}
代码分析:
- jedis.multi();开启Redis的事务
- jedis.watch(result);对result进行监控
- 编写所需命令入队列 multi.set(“user1”,result); multi.set(“user2”,result);
- 执行 multi.exec()时,Redis会执行队列中的命令
这个时候如果事务执行成功,就会finally中就会输出get到的值,
如果事务执行失败,就会进行异常的捕获,同时将监控进行解锁 jedis.unwatch();,并且放弃本次事务 multi.discard(),
最后不管事务执行是否成功,都要将Jedis进行关闭。
运行结果: