1. 程式人生 > >java操作redis的五種資料型別

java操作redis的五種資料型別

一、專案必備Jar包

    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>

    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

二、例項與結構

1)遠端連線redis,清空資料庫                     new Jedis(redis服務連線IP,埠)

  private  static Jedis jedis;

    @Before
    public void before(){
        jedis= JedisConnectUtil.getJedis(JedisTest.URL,63**);
        jedis.flushDB();
    }

2)String型別

    @Test
    public void testString(){
        //設定鍵  獲取鍵   get   set
        jedis.set("steve","stevetao");
        System.out.println("設定後值:"+jedis.get("steve"));
        //追加鍵       append
        jedis.append("steve"," Is Good Man");
        System.out.println("追加後值:"+jedis.get("steve"));
        //刪除操作      del
        jedis.del("steve");
        System.out.println("刪除後值:"+jedis.get("steve"));
        //不存在就儲存,   setnx   msetnx
        jedis.setnx("steve","stevetao");
        System.out.println("設定後值:"+jedis.get("steve"));
        System.out.println("再次設定後值:"+jedis.setnx("steve","stevetao"));
        //擷取字串      substr
        System.out.println("擷取後值:"+jedis.substr("steve",0,4));
        //設定多個鍵值對       mset    mget
        jedis.mset(new String[]{"zhangsan","123","lisi","1234"});
        System.out.println("多次設定後值:"+jedis.mget("zhangsan","lisi"));
        //遞增遞減          incr  decr      incrby  decrby
        jedis.incr("zhangsan");
        jedis.decr("lisi");
        System.out.println("遞增遞減後值:"+jedis.mget("zhangsan","lisi"));
        jedis.incrBy("zhangsan",6);
        jedis.decrBy("lisi",3);
        System.out.println("遞增遞減後值:"+jedis.mget("zhangsan","lisi"));
    }

3)List型別

    @Test
    public void testList(){
        //尾新增 rpush          頭新增  lpush
        jedis.lpush("books","java","C++","Ruby","Scala","python");
        jedis.rpush("language","java","C++","Ruby","Scala","python");
        //  -1 表示列表的最後一個元素, -2 表示列表的倒數第二個元素,以此類推。
        System.out.println("頭新增後books值:"+jedis.lrange("books",0,-1));
        System.out.println("尾新增後language值:"+jedis.lrange("language",0,-1));
        //尾部刪除 rpop   頭部刪除 lpop
        System.out.println("刪除的值為:"+jedis.lpop("books"));
        System.out.println("刪除的值為:"+jedis.rpop("language"));
        System.out.println("頭部刪除後books值:"+jedis.lrange("books",0,-1));
        System.out.println("尾部刪除後language值:"+jedis.lrange("language",0,-1));
        //尾部刪除並頭新增 rpoplpush
        jedis.rpoplpush("language","books");
        System.out.println("尾部刪除並頭新增後books值:"+jedis.lrange("books",0,-1));
        System.out.println("尾部刪除並頭新增後language值:"+jedis.lrange("language",0,-1));
        //區別:  只能給存在的list做新增,不能項lpush那樣能新增list
        jedis.lpushx("books","php");
        jedis.lpushx("book","php");
        System.out.println("頭新增後books值:"+jedis.lrange("books",0,-1));
        System.out.println("頭新增後book值:"+jedis.lrange("book",0,-1));
        //獲取集合長度 llen  指定索引的值 lindex   保留擷取的值 ltrim
        System.out.println("books集合長度:"+jedis.llen("books"));
        System.out.println("books集合第二個數值:"+jedis.lindex("books",1));
        jedis.ltrim("books",0,2);
        System.out.println("擷取後books值:"+jedis.lrange("books",0,-1));
    }

4)Hash型別

    @Test
    public void testHash(){
        //適合欄位:設值hset  取值hget(如果value是json字串,類似儲存物件)
        jedis.hset("student","name","zhangsan");
        System.out.println("student中name的值為:"+jedis.hget("student","name"));
        //適合物件:設值hmset      取值hmget
        Map<String,String> map = new HashMap<String,String>();
        map.put("name", "lisi");
        map.put("age", "36");
        jedis.hmset("teacher",map);
        System.out.println("teacher中name、age的值為:"+jedis.hmget("teacher","name","age"));
        //teacher是否存在鍵age  hexists
        if(jedis.hexists("teacher","age")){
            //給指定值增加4   hincrBy
            jedis.hincrBy("teacher","age",4);
            System.out.println("teacher中name、age的值為:"+jedis.hmget("teacher","name","age"));
        }
        //返回key的個數hlen      返回值hvals    返回鍵hkeys  鍵值對hgetAll
        jedis.hset("student","age","13");
        jedis.hset("student","qq","2246920330");
        jedis.hset("student","address","beijing");
        System.out.println("student中鍵的個數為:"+jedis.hlen("student"));
        System.out.println("student中所有的鍵為:"+jedis.hkeys("student"));
        System.out.println("student中所有的值為:"+jedis.hvals("student"));
        System.out.println("student中所有的鍵值對為:"+jedis.hgetAll("student"));
        //刪除 hdel
        jedis.hdel("student",new String[]{"address","qq","age"});
        System.out.println("刪除後,student中所有的鍵值對為:"+jedis.hgetAll("student"));
    }

5)Set型別

    @Test
    public void testSet(){
        //set中新增值 sadd      取值 smembers
        jedis.sadd("student","Jan","John","Steve","jack","lili","peter","Anna");
        jedis.sadd("girls","Jan","lili","Alice","Jeanne","Anna");
        System.out.println("排名不分先後:"+jedis.smembers("student"));
        //set個數 scard      是否存在某個值 sismember
        System.out.println("set集合的個數:"+jedis.scard("student"));
        System.out.println("student是否存在steve:"+jedis.sismember("student","Steve"));
        System.out.println("student是否存在stevetao:"+jedis.sismember("student","Stevetao"));
        //System.out.println(jedis.sscan("student","0").getResult());
        //刪除指定的值 srem     隨機刪除並返回 spop
        System.out.println("刪除指定的值Steve:"+jedis.srem("student","Steve"));
        System.out.println("刪除的值為:"+jedis.spop("student"));
        System.out.println("再次排名不分先後:"+jedis.smembers("student"));
        //集合操作
        System.out.println("兩個set的交集:"+jedis.sinter("student","girls"));
        System.out.println("兩個set的並集:"+jedis.sunion("student","girls"));
        System.out.println("student對girls的差集:"+jedis.sdiff("student","girls"));
        System.out.println("girls對student的差集:"+jedis.sdiff("girls","student"));
        //集合操作並儲存
        jedis.sinterstore("jiaoji","student","girls");
        jedis.sunionstore("bingji","student","girls");
        jedis.sdiffstore("chaji","student","girls");

        System.out.println("交集:"+jedis.smembers("jiaoji"));
        System.out.println("並集:"+jedis.smembers("bingji"));
        System.out.println("student對girls的差集:"+jedis.smembers("chaji"));
    }

6)ZSet型別

    @Test
    public void testZset(){
        jedis.zadd("math",75,"Jim");
        jedis.zadd("math",86,"Lina");
        jedis.zadd("math",52,"Dive");
        jedis.zadd("math",91,"Bobber");
        System.out.println("有序集合的成員數:"+jedis.zcard("math"));
        System.out.println("有序集合的成員:"+jedis.zrevrangeByScore("math",100,0));
        //返回set<Tuple>
        System.out.println("有序集合的成員:"+jedis.zrangeWithScores("math",0,100));
    }

這些簡單的操作,可以找下API文件,每一個都試一遍。基本上和Linux裡的命令相差不大,如果命令熟了,那麼使用jedis應該很快就能上手了。