5種類のデータ構造のユニットtest---複合パッケージのJedisUtil使用が必要

10133 ワード

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+"]");
    }