五种数据结构的单元test---需复合封装的JedisUtil使用

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();
        //list中左边依次添加成员
        jedis.lpush("person","ninka1");
        jedis.lpush("person","zs2");
        jedis.lpush("person","ls3");
        jedis.lpush("person","ww4");
        //lrange范围输出,0到-1是指全部list
        List personList1 =  jedis.lrange("person",0,-1);
        for (String person : personList1){
            System.out.println(person);
        }
        //删除list
        jedis.del("person");
        //list中右边依次添加成员
        jedis.rpush("person","ninka1");
        jedis.rpush("person","zs2");
        jedis.rpush("person","ls3");
        jedis.rpush("person","ww4");
        //lrange范围输出,0到-1是指全部list
        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(){//有序的set
        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+"]");
        //删除ww
        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+"]");
    }

你可能感兴趣的:(reids)