1、String
@Test
@Test
public void DemoString(){
Jedis jedis = JedisUtil.getJedis();
jedis.set("name","ninka");
String a = jedis.get("name");
System.out.println(a);
JedisUtil.releaseResource(jedis);
}
2、hash
@Test
public void demoHash(){
Jedis jedis = JedisUtil.getJedis();
jedis.hset("hash1","name","ninka");
jedis.hset("hash1","age","24");
System.out.println(jedis.hgetAll("hash1"));
jedis.hincrBy("hash1","age",2);
System.out.println("增加年龄后:"+jedis.hgetAll("hash1"));
jedis.hdel("hash1","age");
System.out.println("删除年龄后:"+jedis.hgetAll("hash1"));
JedisUtil.releaseResource(jedis);
}
3、List
@Test
public void demoList(){
Jedis jedis = JedisUtil.getJedis();
jedis.lpush("person","ninka1");
jedis.lpush("person","zs2");
jedis.lpush("person","ls3");
jedis.lpush("person","ww4");
List personList1 = jedis.lrange("person",0,-1);
for (String person : personList1){
System.out.println(person);
}
jedis.del("person");
jedis.rpush("person","ninka1");
jedis.rpush("person","zs2");
jedis.rpush("person","ls3");
jedis.rpush("person","ww4");
List personList2 = jedis.lrange("person",0,-1);
for (String person : personList2){
System.out.println(person);
}
JedisUtil.releaseResource(jedis);
}
4、Set
@Test
public void demoSet(){
Jedis jedis = JedisUtil.getJedis();
jedis.sadd("myset1","ninka","a","b"," c","d");
jedis.sadd("myset2","ninka", "a", "b", "d");
System.out.println("myset1=" + jedis.smembers("myset1"));
System.out.println("myset2=" + jedis.smembers("myset2"));
String inter = "";
for (String str:jedis.sinter("myset1","myset2")){
inter = inter + str +" ";
}
System.out.println("交集为:"+inter);
String union = "";
for (String str:jedis.sunion("myset1","myset2")){
union = union + str +" ";
}
System.out.println("并集为:"+union);
String diffSet="";
for (String str:jedis.sdiff("myset1","myset2")){
diffSet= diffSet + str + " ";
}
System.out.println("差集是:"+diffSet);
JedisUtil.releaseResource(jedis);
}
5、SortedSet
@Test
public void demoSortedSet(){
Jedis jedis = JedisUtil.getJedis();
jedis.zadd("mySortedSet1",100.0,"ninka");
jedis.zadd("mySortedSet1",80,"zs");
jedis.zadd("mySortedSet1",70,"ls");
jedis.zadd("mySortedSet1",60,"ww");
System.out.println("成员数量为:"+jedis.zcard("mySortedSet1"));
System.out.println("70~100的成员数量为:"+jedis.zcount("mySortedSet1",70,100));
String sort = "";
String str = "";
for (Tuple tuple :jedis.zrangeWithScores("mySortedSet1",0,-1)){
str = tuple.getElement()+","+String.valueOf(tuple.getScore());
sort = sort+str+" ";
}
System.out.println("所有成员的名和分数:["+sort+"]");
jedis.zrem("mySortedSet1","ww");
System.out.println("删除ww之后的成员数量为:"+jedis.zcard("mySortedSet1"));
jedis.zincrby("mySortedSet1",1,"ls");
String sort1 = "";
String str1 = "";
for (Tuple tuple :jedis.zrangeWithScores("mySortedSet1",0,-1)){
str1 = tuple.getElement()+","+String.valueOf(tuple.getScore());
sort1 = sort1+str1+" ";
}
System.out.println("给ls加分后所有成员的名和分数:["+sort1+"]");
}