Redis安装不多说了,上个菜鸟教程,自己搞吧。
https://www.runoob.com/redis/redis-install.html
set key value
get key
localhost:6379> set myName laohu OK localhost:6379> get myName "laohu"
exists key
localhost:6379> exists myName (integer) 1
del key
localhost:6379> del myName (integer) 1
type key
localhost:6379> set obj hello OK localhost:6379> type obj string
mset k1 v1 k2 v2 k3 v3...
mget k1 k2 k3 ...
localhost:6379> mset k1 v1 k2 v2 OK localhost:6379> mget k1 k2 1) "v1" 2) "v2"
只列简单的,复杂命令看这里:
https://www.cnblogs.com/tg666/p/11598437.html
加入pom.xml依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency>
public class redisTest { public static void main(String[] args) { //创建对象,连接 Jedis jedis = new Jedis("127.0.0.1", 6379); //连接测试 System.out.println(jedis.ping()); } }
运行结果:
PONG Process finished with exit code 0
public class redisTest2 { public static void main(String[] args) { //创建对象,连接 Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("清空当前数据库: "+jedis.flushDB()); System.out.println("判断: "+jedis.exists("k1")); System.out.println("添加: "+jedis.set("k1", "v1")); System.out.println("添加: "+jedis.set("k2", "v2")); Set<String> keys = jedis.keys("*"); System.out.println("列出所有key: "+keys); System.out.println("删除: "+jedis.del("k1")); System.out.println("判断: "+jedis.exists("k1")); System.out.println("查看键对应的值的类型: "+jedis.type("k2")); System.out.println("随机返回一个key: "+jedis.randomKey()); System.out.println("重命名key: "+jedis.rename("k2", "k3")); System.out.println("获取元素: "+jedis.get("k3")); System.out.println("根据索引查询: "+jedis.select(0)); System.out.println("清空当前数据库: "+jedis.flushDB()); System.out.println("数据库key数量: "+jedis.dbSize()); System.out.println("清空所有数据库的key: "+jedis.flushAll()); } }
运行结果:
清空当前数据库: OK 判断: false 添加: OK 添加: OK 列出所有key: [k1, k2] 删除: 1 判断: false 查看键对应的值的类型: string 随机返回一个key: k2 重命名key: OK 获取元素: v2 根据索引查询: OK 清空当前数据库: OK 数据库key数量: 0 清空所有数据库的key: OK Process finished with exit code 0
System.out.println("添加元素"); System.out.println(jedis.set("k1", "v1")); System.out.println(jedis.set("k2", "v2")); System.out.println(jedis.set("k3", "v3")); System.out.println("删除: "+jedis.del("k2")); System.out.println("取值: "+jedis.get("k2")); System.out.println("修改值: "+jedis.set("k1", "v11")); System.out.println("取值: "+jedis.get("k1")); System.out.println("追加值: "+jedis.append("k3", "end")); System.out.println("取值: "+jedis.get("k3")); System.out.println("添加多个元素: "+jedis.mset("k4","v4","k5","v5")); System.out.println("获取多个值: "+jedis.mget("k4","k5","k6")); System.out.println("删除多个元素: "+jedis.del("k4","k5")); System.out.println("获取多个值: "+jedis.mget("k3","k4","k5")); System.out.println("清空: "+jedis.flushDB()); System.out.println("新增键值对,防止被覆盖"); System.out.println("第一次赋值: "+jedis.setnx("k1", "v1")); System.out.println("第一次赋值: "+jedis.setnx("k2", "v2")); System.out.println("覆盖原值: "+jedis.setnx("k2", "v2new")); System.out.println("取值: "+jedis.get("k1")); System.out.println("取值: "+jedis.get("k2")); System.out.println("添加元素设置生命周期: "+jedis.setex("k3", 2, "v3")); System.out.println("取值: "+jedis.get("k3")); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("过期后取值: "+jedis.get("k3")); System.out.println("获取原值,并更新: "+jedis.getSet("k2", "v2abc")); System.out.println("获取新的值: "+jedis.get("k2")); System.out.println("截取部分字符串: "+jedis.getrange("k2", 2, 4));
运行结果:
添加元素 OK OK OK 删除: 1 取值: null 修改值: OK 取值: v11 追加值: 5 取值: v3end 添加多个元素: OK 获取多个值: [v4, v5, null] 删除多个元素: 2 获取多个值: [v3end, null, null] 清空: OK 新增键值对,防止被覆盖 第一次赋值: 1 第一次赋值: 1 覆盖原值: 0 取值: v1 取值: v2 添加元素设置生命周期: OK 取值: v3 过期后取值: null 获取原值,并更新: v2 获取新的值: v2abc 截取部分字符串: abc Process finished with exit code 0
System.out.println("清空数据: "+jedis.flushDB()); System.out.println("新增元素"); System.out.println(jedis.lpush("list", "a","b","c","d")); System.out.println(jedis.lpush("list", "d")); System.out.println(jedis.lpush("list", "e")); System.out.println(jedis.lpush("list", "f")); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("查看区间数据: "+jedis.lrange("list", 0, 3)); System.out.println("删除列表中指定的值,第二参数为删除的个数(有重复时),后进先出原则,相当于出栈"); System.out.println("删除指定值的元素: "+jedis.lrem("list", 2, "d")); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("删除区间以外的元素: "+jedis.ltrim("list", 0, 3)); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("左侧弹出: "+jedis.lpop("list")); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("右侧新增: "+jedis.rpush("list", "g")); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("右侧弹出: "+jedis.rpop("list")); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("修改指定下标元素: "+jedis.lset("list", 1, "new")); System.out.println("查看所有: "+jedis.lrange("list", 0, -1)); System.out.println("队列长度: "+jedis.llen("list")); System.out.println("获取指定下标元素: "+jedis.lindex("list", 2)); System.out.println("新增元素: "+jedis.lpush("list1", "4","2","0","6","5","8")); System.out.println("查看所有: "+jedis.lrange("list1", 0, -1)); System.out.println("排序: "+jedis.sort("list1")); System.out.println("查看所有: "+jedis.lrange("list1", 0, -1));
运行结果:
清空数据: OK 新增元素 4 5 6 7 查看所有: [f, e, d, d, c, b, a] 查看区间数据: [f, e, d, d] 删除列表中指定的值,第二参数为删除的个数(有重复时),后进先出原则,相当于出栈 删除指定值的元素: 2 查看所有: [f, e, c, b, a] 删除区间以外的元素: OK 查看所有: [f, e, c, b] 左侧弹出: f 查看所有: [e, c, b] 右侧新增: 4 查看所有: [e, c, b, g] 右侧弹出: g 查看所有: [e, c, b] 修改指定下标元素: OK 查看所有: [e, new, b] 队列长度: 3 获取指定下标元素: b 新增元素: 6 查看所有: [8, 5, 6, 0, 2, 4] 排序: [0, 2, 4, 5, 6, 8] 查看所有: [8, 5, 6, 0, 2, 4]
jedis.flushDB(); System.out.println("新增元素,不可重复"); System.out.println(jedis.sadd("set1", "s0","s1","s2","s3","s4","s5","s7","s8")); System.out.println(jedis.sadd("set1", "s6")); System.out.println(jedis.sadd("set1", "s6")); System.out.println("列出所有元素: "+jedis.smembers("set1")); System.out.println("删除一个元素: "+jedis.srem("set1", "s0")); System.out.println("列出所有元素: "+jedis.smembers("set1")); System.out.println("删除元素: "+jedis.srem("set1", "s6","s7")); System.out.println("随机移除一个元素: "+jedis.spop("set1")); System.out.println("随机移除一个元素: "+jedis.spop("set1")); System.out.println("列出所有元素: "+jedis.smembers("set1")); System.out.println("查看元素个数: "+jedis.scard("set1")); System.out.println("判断是否存在某个元素: "+jedis.sismember("set1", "s3")); System.out.println("判断某个元素是否存在: "+jedis.sismember("set1", "s1")); System.out.println(jedis.sismember("set1", "s5")); System.out.println("================"); System.out.println("新增: "+jedis.sadd("set2", "s0","s1","s2","s4","s5","s7","s8")); System.out.println("新增: "+jedis.sadd("set3", "s0","s1","s2","s4","s8")); System.out.println("set2中删除,并存到另一集合上 : "+jedis.smove("set2", "set3", "s1")); System.out.println("set2中删除,并存到另一集合上 : "+jedis.smove("set2", "set3", "s1")); System.out.println("删除元素并放在新集合中 :"+jedis.smove("set2", "set3", "s2")); System.out.println("列出所有元素: "+jedis.smembers("set2")); System.out.println("列出所有元素: "+jedis.smembers("set3")); System.out.println("交集: "+jedis.sinter("set2","set3")); System.out.println("并集: "+jedis.sunion("set2","set3")); System.out.println("差集: "+jedis.sdiff("set2","set3")); System.out.println("交集保存到集合中: "+jedis.sinterstore("set4", "set2","set3")); System.out.println("列出所有元素: "+jedis.smembers("set4"));
运行结果:
新增元素,不可重复 8 1 0 列出所有元素: [s8, s5, s4, s1, s6, s7, s3, s0, s2] 删除一个元素: 1 列出所有元素: [s8, s5, s4, s1, s6, s7, s3, s2] 删除元素: 2 随机移除一个元素: s2 随机移除一个元素: s8 列出所有元素: [s5, s4, s1, s3] 查看元素个数: 4 判断是否存在某个元素: true 判断某个元素是否存在: true true ================ 新增: 7 新增: 5 set2中删除,并存到另一集合上 : 1 set2中删除,并存到另一集合上 : 0 删除元素并放在新集合中 :1 列出所有元素: [s8, s5, s4, s7, s0] 列出所有元素: [s8, s4, s1, s0, s2] 交集: [s8, s4, s0] 并集: [s8, s1, s4, s5, s0, s7, s2] 差集: [s5, s7] 交集保存到集合中: 3 列出所有元素: [s8, s4, s0]
System.out.println("清空当前数据库"); jedis.flushDB(); Map<String,String> map = new HashMap(); map.put("k1", "v1"); map.put("k2", "v2"); map.put("k3", "v3"); map.put("k4", "v4"); System.out.println("添加map集合"); jedis.hmset("hash", map); System.out.println("添加元素: "+jedis.hset("hash", "k5","v5")); System.out.println("所有键值对: "+jedis.hgetAll("hash")); System.out.println("所有键: "+jedis.hkeys("hash")); System.out.println("所有值: "+jedis.hvals("hash")); System.out.println("给一个键保存整数,如果该键不存在则添加: "+jedis.hincrBy("hash", "k6", 6)); System.out.println("所有键值对: "+jedis.hgetAll("hash")); System.out.println("添加或修改: "+jedis.hincrBy("hash", "k6", 60)); System.out.println("所有键值对: "+jedis.hgetAll("hash")); System.out.println("删除一个或多个元素: "+jedis.hdel("hash", "k2")); System.out.println("所有元素: "+jedis.hgetAll("hash")); System.out.println("元素个数: "+jedis.hlen("hash")); System.out.println("判断元素是否存在: "+jedis.hexists("hash", "k2")); System.out.println("判断元素是否存在: "+jedis.hexists("hash", "k3")); System.out.println("根据键获取值: "+jedis.hmget("hash", "k3")); System.out.println("根据键获取值: "+jedis.hmget("hash", "k3","k4"));
运行结果:
清空当前数据库 添加map集合 添加元素: 1 所有键值对: {k3=v3, k4=v4, k5=v5, k1=v1, k2=v2} 所有键: [k3, k4, k5, k1, k2] 所有值: [v4, v3, v2, v1, v5] 给一个键保存整数,如果该键不存在则添加: 6 所有键值对: {k3=v3, k4=v4, k5=v5, k6=6, k1=v1, k2=v2} 添加或修改: 66 所有键值对: {k3=v3, k4=v4, k5=v5, k6=66, k1=v1, k2=v2} 删除一个或多个元素: 1 所有元素: {k3=v3, k4=v4, k5=v5, k6=66, k1=v1} 元素个数: 5 判断元素是否存在: false 判断元素是否存在: true 根据键获取值: [v3] 根据键获取值: [v3, v4]
JSONObject jsonObject = new JSONObject(); jsonObject.put("hello", "world"); jsonObject.put("hi", "Java"); jedis.flushDB(); System.out.println("开启事务"); Transaction multi = jedis.multi(); String result = jsonObject.toJSONString(); try { multi.set("k1", result); multi.set("k2", result); // 模拟异常 // int i = 1/0; multi.exec();//提交事务 } catch (Exception e) { multi.discard();//回滚事务 e.printStackTrace(); } finally { System.out.println(jedis.get("k1")); System.out.println(jedis.get("k2")); jedis.close();//关闭连接 }
运行结果:
开启事务 {"hi":"Java","hello":"world"} {"hi":"Java","hello":"world"}
以上API部分案例引用下文,复杂的用法也可以参考以下文章:
https://blog.csdn.net/weixin_47257749/article/details/114044856