redis simple demo

1. Sample code:
//Refer to http://blog.csdn.net/wilsonke/article/details/47376407
  Jedis redis = new Jedis ("127.0.0.1",6379);//Connect redis
        //The default operation is db0
       
        /* ----------------------------------------------- -------------------------------------------------- ---------- */  
        /*STRING operation
        redis.set("name", "wangjun1");  
        redis.set("id", "123456");  
        redis.set("address", "guangzhou");
       
        //SETEX key seconds value associates the value value with the key, and sets the key's lifetime to seconds (in seconds). After the lifetime expires, the key value will be deleted in the redis library. Do not set the time-to-live key value to take effect permanently
        redis.setex("foo", 15, "haha");
       
        //MSET key value [key value ...   ] Simultaneously set one or more key-value pairs.
        redis.mset("haha","111","xixi","222"); 

        // redis.flushAll();//Clear all keys  
        System.out.println(redis.dbSize());//dbSize is the number of keys 
       
        //APPEND key value if the key already exists and is a character string, the APPEND command appends the value to the original value of the key.  
        redis.append("foo", "00");//If the key already exists and is a string, the APPEND command appends the value to the original value of the key.  
          
        //GET key returns the string value associated with key  
        System.out.println(redis.get("foo"));
      
       //MGET key [key ...] returns all (one or more) given keys List of values  
       ​​list = redis.mget("haha","xixi");  
       for(int i=0;i<list.size();i++){  
           System.out.println(list.get(i));  



        // KEY operation
       /* Set keys = redis.keys("*");//List all keys, find a specific key such as: redis.keys("foo")  
        Iterator t1=keys.iterator() ;  
        while (t1.hasNext()){  
            Object obj1=t1.next();  
            System.out.println(obj1);  
        }   
        //DEL removes the given key or keys. If the key does not exist, the command is ignored.  
        redis.del("name1");  
        //TTL returns the remaining time to live for the given key (in seconds) When the key does not exist, returns -2. When the key exists but the remaining lifetime is not set (permanently valid), return -1
       System.out.println(redis.ttl("foo"));  
       //PERSIST key removes the lifetime of the given key.  
       redis.persist("foo");
       System.out.println(redis.ttl("foo"));

      System.out.println(redis.exists("foo"));
       //MOVE key db moves the key of the current database (default 0) to the given database db. MOVE has no effect if the current database (source database) and the given database (target database) have the given key with the same name, or if the key does not exist in the current database.  
       redis.move("foo", 1);//Move the key foo to database 1
       //RENAME key newkey Change the key to newkey. Returns an error when key and newkey are the same or key does not exist. When newkey already exists, the RENAME command will overwrite the old value.  
       //redis.rename("foo", "foonew");
      
       //TYPE key returns the type of the value stored by key.  
       System.out.println(redis.type("foo"));//none(key does not exist), string(string), list(list), set(set), zset(ordered set), hash( Hash table)  
       //EXPIRE key seconds Set the time-to-live for the given key. When the key expires, it is automatically deleted.  
       redis.expire("foo", 5);//Expires in 5 seconds  
      
       //General SORT usage The easiest way to use SORT is the SORT key.  
       redis.lpush("  
       redis.lpush("sort", "4");  
       redis.lpush("sort", "6");  
       redis.lpush("sort", "3");  
       redis.lpush("sort", "0");  
      
      
       List list = redis.sort("sort");//默认是升序  
       for(int i=0;i<list.size();i++){  
           System.out.println(list.get(i));  
       }   */
       
       /***********************************************************************************************************/
        //  Hash 操作  
       
        //HSET key field value将哈希表key中的域field的值设为value。  
       /* redis.hset("website", "google", "www.google.cn");  
        redis.hset("website", "baidu", "www.baidu.com");  
        redis.hset("website", "sina", "www.sina.com");  
          
        //HMSET key field value [field value ...] Simultaneously set multiple field-value (field-value) pairs to the hash table key.  
        Map map = new HashMap();  
        map.put("cardid", "123456");  
        map.put("username", "jzkangta");  
        redis.hmset("hash", map);  
          
        //HGET key field Returns the value of the given field field in the hash table key.  
        System.out.println(redis.hget("hash", "username"));  
          
        //HMGET key field [field ...] returns the value of one or more given fields in the hash table key.  
        List list = redis.hmget("website","google","baidu","sina");  
        for(int i=0;i<list.size();i++){  
            System.out.println(list. get(i));  

          

        Map<String,String> map1 = redis.hgetAll("hash");  
        for(Map.Entry entry: map1.entrySet()) {  
             System.out.print(entry.getKey() + ":" + entry.getValue () + "\t");  
        } */ 
          
        //HDEL key field [field ...] Delete one or more specified fields in the hash table key.  
        //HLEN key returns the number of fields in the hash table key.  
        //HEXISTS key field Check whether the given field field exists in the hash table key.  
        //HINCRBY key field increment is the value of the field field in the hash table key plus increment increment.  
        //HKEYS key returns all fields in the hash table key.  
        //HVALS key returns all the values ​​in the hash table key.  
           
         /**************************************************** ************************************************/     
        // LIST operate  
        //LPUSH key value [value ...] inserts the value value into the header of the list key.  
    /* redis.lpush("list", "abc");  
        redis.lpush("list", "xzc");  
        redis.lpush("list", "erf");  
        redis.lpush("list", " bnh");  
          
        //LRANGE key start stop returns the elements in the specified interval in the list key, and the interval is specified by offset start and stop. The subscript (index) parameters start and stop are both base 0, that is, 0 represents the first element of the list, 1 represents the second element of the list, and so on. You can also use negative subscripts, with -1 for the last element of the list, -2 for the second-to-last element of the list, and so on.  
        List list = redis.lrange("list", 0, -1);  
        for(int i=0;i<list.size();i++){  
            System.out.println(list.get(i));  
        } */
          
        //LLEN key returns the length of the list key.  
        //LREM key count value According to the value of the parameter count, remove the elements in the list that are equal to the parameter value.  
           
       
        // SET operation  
        //SADD key member [member ...] adds the member element to the set key.  
        redis.sadd("testSet", "s1");  
        redis.sadd("testSet", "s2");  
        redis.sadd("testSet", "s3");  
        redis.sadd("testSet", "s4" );  
        redis.sadd("testSet", "s5");  
          
        //SREM key member removes the member element in the set.  
        redis.srem("testSet", "s5");  
          
        //SMEMBERS key returns all members in the set key.  
        Set set = redis.smembers("testSet");  
        Iterator t1=set.iterator() ;  
        while(t1.hasNext()){  
            Object obj1=t1.next();  
            System.out.  

          
        //SISMEMBER key member Determines whether the member element is a member of the set key. Yes (true), otherwise (false)  
        System.out.println(redis.sismember("testSet", "s4"));  
          
        //SCARD key returns the cardinality of the set key (the number of elements in the set).  
        //SMOVE source destination member moves the member element from the source collection to the destination collection.  
           
        //SINTER key [key ...] returns all members of a set that is the intersection of all the given sets.  
        //SINTERSTORE destination key [key ...] This command is equivalent to SINTER, but it saves the result to the destination collection instead of simply returning the result set  
        //SUNION key [key ...] returns all members of a collection, This set is the union of all the given sets.  
        //SUNIONSTORE destination key [key ...] This command is equivalent to SUNION, but it saves the result to the destination collection instead of simply returning the result set.  
        //SDIFF key [key ...] returns all members of a set that is the difference of all the given sets.  
        //SDIFFSTORE destination key [key ...] This command is equivalent to SDIFF, but it saves the result to the destination collection instead of simply returning the result set.  
2. Explain
  that the jedis-2.7.0.jar package needs to be introduced

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326926377&siteId=291194637