Simple operation of Jedis Redis List

import redis.clients.jedis.Jedis;

public class Test {

	public static void main(String[] args) {
		
		Jedis jedis = new Jedis("10.110.20.152",6379);
		String cacheKey = "test";
		
		//Return the length of the list stored at the specified key.
		//If the key does not exist zero is returned .
		//If the value stored at key is not a list an error is returned.
		long length = jedis.llen(cacheKey);
		System.out.println(length);//输出0
		
		//Add the string value to the head (LPUSH) or tail (RPUSH)
		//of the list stored at key.
		//If the key does not exist an empty list is created
		//just before the append operation.
		//If the key exists but is not a List an error is returned.
		jedis.rpush(cacheKey, "b");//Insert from the end of the queue
		jedis.rpush(cacheKey, "c");//Insert from the end of the queue
		jedis.lpush(cacheKey, "a");//Insert from the head of the team
		System.out.println(jedis.llen(cacheKey));//Output 3
		
		//Atomically return and remove
		//the first (LPOP) or last (RPOP) element of the list.
		//For example if the list contains the elements "a","b","c"
		//LPOP will return "a" and the list will become "b","c".
		String value1 = jedis.lpop(cacheKey);//Pop up from the head of the team
		System.out.println(jedis.llen(cacheKey));//Output 2
		String value2 = jedis.lpop(cacheKey);//Pop up from the head of the team
		System.out.println(jedis.llen(cacheKey));//Output 1
		String value3 = jedis.lpop(cacheKey);//Pop up from the head of the team
		System.out.println(jedis.llen(cacheKey));//Output 0
		System.out.println(value1+value2+value3);//输出abc
		System.out.println(jedis.lpop(cacheKey));//输出null
		
		jedis.close();
	}

}

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326227279&siteId=291194637