使用jedis操作redis常用方法

时间:2022-08-13 13:52:59

在redis入门及在商城案例中的使用中简单介绍了下使用jedis如何操作redis,但是其实方法是跟redis的操作大部分是相对应的。我这里做下记录

1、String类型操作
public class StringTest { Jedis jedis;
@Before//该注解下的方法在@Test执行之前执行
public void before(){
//创建连接
jedis = new Jedis("192.168.25.128",6379);
}
@After//该注解下的方法在@Test执行之后执行
public void after(){
//关闭连接
jedis.close();
}
/*
* 存储,获取,设置过期时间,key命令
*/
@Test
public void test() throws InterruptedException{
//1.存储值
jedis.set("name", "张三");
//2.取值
String name = jedis.get("name");
System.out.println(name);
//3.key命令:查看有效期,-1表示持久化
Long t = jedis.ttl("name");
System.out.println(t);
//4.key命令,对已经存在的key设置过期时间
jedis.expire("name", 5);
while(true){
String name2 = jedis.get("name");
System.out.println(name2);
System.out.println("有效期为:"+jedis.ttl("name")+"秒");
Thread.sleep(2000);
} }
输出结果:
张三
-1
-------------------
张三
有效期为:5秒
张三
有效期为:3秒
张三
有效期为:1秒
null
有效期为:-2秒
/*
* 测试自增自减:前提,value值能解析为数字类型
* 删除
*/
@Test
public void test2(){
jedis.set("age", "18");
String age1 = jedis.get("age");
System.out.println("当前年龄:"+age1); Long age2 = jedis.decr("age");
System.out.println("一年前年龄为:"+age2); Long age3 = jedis.incr("age");
System.out.println("现在年龄又变回为:"+age3); //如果key不存在则忽略此操作
Long del = jedis.del("age1");
}
输出结果:
当前年龄:18
一年前年龄为:17
现在年龄又变回为:18
/*
* 批量存储和获取
*/
@Test
public void test3(){
jedis.mset("a1","mysql","a2","oracle","a3","sqlServer","a4",
"redis","a5","mongodb","a6","hbase");
List list = jedis.mget("a1","a2","a3","a4","a5","a6");
for (String s : list) {
System.out.println(s);
}
}
输出结果:
mysql
oracle
sqlServer
redis
mongodb
hbase
/*
* 存储值的同时设置过期时间,判断key是否存在
*/
@Test
public void test4() throws InterruptedException{
//jedis.setex(key, 过期时间, value)
jedis.setex("life", 5, "享受美好");
while(jedis.exists("life")){
System.out.println(jedis.get("life"));
Thread.sleep(2000);
}
}
输出结果:在输出三个“享受美好”之后便不再输出
享受美好
享受美好
享受美好
2、Hash类型操作
public class MapTest { Jedis jedis;
@Before
public void before(){
jedis = new Jedis("192.168.25.128",6379);
}
@After
public void after(){
jedis.close();
} @Test
public void test1(){
//1.存储值
jedis.hset("student", "name", "小李");
jedis.hset("student", "class", "小学生");
jedis.hset("student", "age", "10");
jedis.hset("student", "skill", "keng");
//2.获取指定值 获取名字
String name = jedis.hget("student", "name");
System.out.println(name);
System.out.println("----------------------");
//3.获取存储的map
Map<string, string=""> all = jedis.hgetAll("student");
Set<entry<string,string>> set = all.entrySet();
Iterator<entry<string, string="">> iterator = set.iterator();
while (iterator.hasNext()) {
Entry<string, string=""> entry = iterator.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}
System.out.println("------------------------------");
//4.获取map中全部key
Set keySet = jedis.hkeys("student");
for (String key : keySet) {
System.out.println(key);
}
System.out.println("-------------------------------");
//5.获取map中全部values
List list = jedis.hvals("student");
for (String value : list) {
System.out.println(value);
}
System.out.println("------------------");
//6.删除指定的值 删除map中class,name两对键值对
Long long1 = jedis.hdel("student", "class","name");
Set set2 = jedis.hkeys("student");
for (String key : set2) {
System.out.println(key);
}
System.out.println("-------------------");
//7.判断map是否存在
Boolean e = jedis.hexists("student", "class");
System.out.println("class是否存在:"+e);
System.out.println("-----------------");
//8.自增自减,可以指定增加减少的数值
jedis.hincrBy("student", "age", 2);
System.out.println(jedis.hget("student", "age"));
}
}
输出结果:
小李
----------------------
skill:keng
name:小李
class:小学生
age:10
------------------------------
name
class
age
skill
-------------------------------
10
keng
小李
小学生
------------------
age
skill
-------------------
class是否存在:false
-----------------
2年后年龄为:12
String跟Hash是最常见的类型,在实习这段时间也都只用到这两种,所以我觉得不管怎样这两种至少应该掌握。 3、List类型操作
public class ListTest {
Jedis jedis;
@Before
public void before(){
jedis = new Jedis("192.168.25.128",6379);
}
@After
public void after(){
jedis.close();
} @Test
public void test(){
//1.存储值(左边开始)。当成栈(子弹匣),先进先出,入栈
jedis.lpush("scores", "100","90","80","70","60");
//右边开始存
jedis.rpush("scores", "50","40","30","20","10");
//2.取值(左边开始),可以说是同时移除了该值,出栈
String lv = jedis.lpop("scores");
//右边开始取
String rv= jedis.rpop("scores");
System.out.println("左边取为:"+lv);
System.out.println("右边取为:"+rv);
System.out.println("--------------------");
//3.取所有值(只有左边开始取)0 -1表示取所有位置,位置是[start,end]
//redis命令行操作的时候已经演示过
List list = jedis.lrange("scores", 0, -1);
for (String value : list) {
System.out.println(value);
}
System.out.println("-----------");
//4.插队,44插入到100后面,注:没有什么rinsert()方法
jedis.linsert("scores",BinaryClient.LIST_POSITION .AFTER, "100", "44");
List list2 = jedis.lrange("scores", 0, -1);
for (String value : list2) {
System.out.println(value);
}
List list3 = jedis.lrange("scores", 0, 3);
for (String value : list3) {
System.out.println(value);
} }
}
输出结果:
左边取为:60
右边取为:10
--------------------
70
80
90
100
50
40
30
20
-----------
70
80
90
100
44
50
40
30
20
---------------
70
80
90
100
4、Set类型操作
public class JedisTest {
Jedis jedis;
@Before
public void before(){
jedis = new Jedis("192.168.25.128",6379);
}
@After
public void after(){
jedis.close();
}
/*
* 测试添加,取值,移除,判断
*/
@Test
public void test1(){
//1.存储
jedis.sadd("names", "Tom","Jack","Harry","Lucy","laowang");
//2.获取set中全部记录,取出来的跟存储的顺序不一样
Set members = jedis.smembers("names");
for (String member : members) {
System.out.println(member);
}
//3.移除指定数据
jedis.srem("names","Tome","Jack");
//4.判断某值是否为set中成员
Boolean tom = jedis.sismember("names", "Tome");
System.out.println("Tom在名单里吗:"+tom);
}
输出结果:
Jack
Harry
Lucy
Tom
laowang
Tom在名单里吗:false
/*
* 差集,交集,并集
*/
@Test
public void test2(){
jedis.sadd("set1", "a","b","c","d");
jedis.sadd("set2", "b","c","d","e");
//1.差集 set1中有set2中没有的
Set sdiff = jedis.sdiff("set1","set2");
for (String d : sdiff) {
System.out.println(d);
}
System.out.println("----------------");
//2.交集
Set sinter = jedis.sinter("set1","set2");
for (String in : sinter) {
System.out.println(in);
}
System.out.println("----------------");
//3.并集
Set sunion = jedis.sunion("set1","set2");
for (String un : sunion) {
System.out.println(un);
}
}
输出结果:
a
----------------
d
c
b
----------------
d
e
a
c
b
5、Zset(有序Set)类型操作
public class SortedSetTest {
Jedis jedis;
@Before
public void before(){
jedis = new Jedis("192.168.25.128",6379);
}
@After
public void after(){
jedis.close();
}
@Test
public void test1(){
//1.添加
jedis.zadd("table", 1, "a");
jedis.zadd("table", 3, "b");
jedis.zadd("table", 2, "c");
jedis.zadd("table", 5, "d");
jedis.zadd("table", 4, "e");
//2.取值 0 -1表示取所有,可以自己指定开始结束位置,跟list一样
//默认根据分数由低到高排
Set table = jedis.zrange("table", 0, -1);
for (String t : table) {
System.out.println(t);
}
System.out.println("-----------------");
//3.排序,由高到低排
Set table2 = jedis.zrevrange("table", 0, -1);
for (String t : table2) {
System.out.println(t);
}
System.out.println("-----------------");
//4.修改某个值的分数
jedis.zincrby("table", 7, "a");
Set table3 = jedis.zrevrange("table", 0, -1);
for (String t : table3) {
System.out.println(t);
}
}
}
输出结果:
a
c
b
e
d
-----------------
d
e
b
c
a
-----------------
a
d
e
b
c