Using Jedis to operate Redis in Java

Using Java to operate Redis requires jedis-2.1.0.jar, download address: http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip

If you need to use Redis connection pool, you also need commons-pool-1.5.4.jar, download address: http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip

 
package com.test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Jedis;

public class TestRedis {
    private Jedis jedis;
    
    @Before
    public void setup() {
        //Connect to redis server, 192.168.0.100:6379
        jedis = new Jedis("192.168.0.100", 6379);
        //authorization authentication
        jedis.auth("admin");  
    }
    
    /**
     * redis store string
     */
    @Test
    public void testString() {
        //-----adding data----------  
        jedis.set("name","xinxin");//Put value-->xinxin into key-->name  
        System.out.println(jedis.get("name"));//Execution result: xinxin  
        
        jedis.append("name", " is my lover"); //拼接
        System.out.println(jedis.get("name"));
        
        jedis.del("name"); //Delete a key
        System.out.println(jedis.get("name"));
        // set multiple key-value pairs
        jedis.mset("name","liuling","age","23","qq","476777XXX");
        jedis.incr("age"); //Add 1 operation
        System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq"));
        
    }
    
    /**
     * redis operation Map
     */
    @Test
    public void testMap() {
        //-----adding data----------  
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "xinxin");
        map.put("age", "22");
        map.put("qq", "123456");
        jedis.hmset("user",map);
        //Take out the name in the user, and execute the result: [minxr]--> Note that the result is a generic List  
        //The first parameter is the key of the map object stored in redis, followed by the key of the object put into the map, and the following keys can be followed by multiple, variable parameters  
        List<String> rsmap = jedis.hmget("user", "name", "age", "qq");
        System.out.println(rsmap);  
  
        // delete a key in the map  
        jedis.hdel("user","age");
        System.out.println(jedis.hmget("user", "age")); //Because it is deleted, it returns null  
        System.out.println(jedis.hlen("user")); //Return the number of values ​​stored in the key whose key is user 2
        System.out.println(jedis.exists("user"));//Whether there is a record with key as user returns true  
        System.out.println(jedis.hkeys("user"));//Return all keys in the map object  
        System.out.println(jedis.hvals("user"));//Return all values ​​in the map object
  
        Iterator<String> iter=jedis.hkeys("user").iterator();  
        while (iter.hasNext()){  
            String key = iter.next();  
            System.out.println(key+":"+jedis.hmget("user",key));  
        }  
    }
    
    /**
     * jedis operation List
     */  
    @Test  
    public void testList(){  
        // Before starting, remove all content  
        jedis.del("java framework");  
        System.out.println(jedis.lrange("java framework",0,-1));  
        //First store three pieces of data in the key java framework  
        jedis.lpush("java framework","spring");  
        jedis.lpush("java framework","struts");  
        jedis.lpush("java framework","hibernate");  
        // Then take out all the data jedis.lrange is taken out by range,  
        // The first is the key, the second is the start position, the third is the end position, jedis.llen gets the length -1 means get all  
        System.out.println(jedis.lrange("java framework",0,-1));  
        
        jedis.del("java framework");
        jedis.rpush("java framework","spring");  
        jedis.rpush("java framework","struts");  
        jedis.rpush("java framework","hibernate");
        System.out.println(jedis.lrange("java framework",0,-1));
    }  
    
    /**
     * jedis operation Set
     */  
    @Test  
    public void testSet(){  
        //Add to  
        jedis.sadd("user","liuling");  
        jedis.sadd("user","xinxin");  
        jedis.sadd("user","ling");  
        jedis.sadd("user","zhangxinxin");
        jedis.sadd("user","who");  
        // remove noname  
        jedis.srem("user","who");  
        System.out.println(jedis.smembers("user"));//Get all added values  
        System.out.println(jedis.sismember("user", "who"));//Determine whether who is an element of the user set  
        System.out.println(jedis.srandmember("user"));  
        System.out.println(jedis.scard("user"));//Returns the number of elements in the collection  
    }  
  
    @Test  
    public void test() throws InterruptedException {  
        //jedis sort  
        //Note that rpush and lpush here are List operations. is a doubly linked list (but from a performance point of view)  
        jedis.del("a");//Clear data first, then add data for testing  
        jedis.rpush("a", "1");  
        jedis.lpush("a","6");  
        jedis.lpush("a","3");  
        jedis.lpush("a","9");  
        System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1]  
        System.out.println(jedis.sort("a")); //[1, 3, 6, 9] //Enter the sorted result  
        System.out.println(jedis.lrange("a",0,-1));  
    }  
    
    @Test
    public void testRedisPool() {
        RedisUtil.getJedis().set("newname", "Chinese test");
        System.out.println(RedisUtil.getJedis().get("newname"));
    }
}
 

Guess you like

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