Conquer Redis + Jedis + Spring - List Operation

Posted by Qense on Mon, 09 Sep 2019 07:50:49 +0200

Links to the original text: https://my.oschina.net/mohaiyong/blog/221261

At first, I thought that operating hash tables and lists under Spring was so dirty. In a trance, I found that "stringRedisTemplate.opsForList()" is powerful, and I took time to mend it.

  

Related links:

Conquer Redis

Conquer Redis + Jedis

Conquer Redis + Jedis + Spring (1) - Configuration & General Operation (GET SET DEL)

Conquer Redis + Jedis + Spring (2) - Hash Table Operation (HMGET HMSET)

Conquer Redis + Jedis + Spring (3) - List Operation

 

LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH commands are completed through spring-data-redis. In fact, there are some commands that are not supported in the current version.However, these List methods are sufficient to achieve the normal operation of queues and stacks.

For ease of operation, I used String RedisTemplate. Show with string manipulation. Of course, you can continue to use RedisTemplate.

Less gossip, more code, at a glance:

 

/**
 * Mar 5, 2013
 */
package org.zlex.redis.support;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @author snowolf
 * @version 1.0
 * @since 1.0
 */
@Component("listOps")
public class ListOps {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	/**
	 * Pressing stack
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Long push(String key, String value) {
		return stringRedisTemplate.opsForList().leftPush(key, value);
	}

	/**
	 * Stack out
	 * 
	 * @param key
	 * @return
	 */
	public String pop(String key) {
		return stringRedisTemplate.opsForList().leftPop(key);
	}

	/**
	 * Join the team
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Long in(String key, String value) {
		return stringRedisTemplate.opsForList().rightPush(key, value);
	}

	/**
	 * Team out
	 * 
	 * @param key
	 * @return
	 */
	public String out(String key) {
		return stringRedisTemplate.opsForList().leftPop(key);
	}

	/**
	 * Stack/queue length
	 * 
	 * @param key
	 * @return
	 */
	public Long length(String key) {
		return stringRedisTemplate.opsForList().size(key);
	}

	/**
	 * Scope Retrieval
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public List<String> range(String key, int start, int end) {
		return stringRedisTemplate.opsForList().range(key, start, end);
	}

	/**
	 * remove
	 * 
	 * @param key
	 * @param i
	 * @param value
	 */
	public void remove(String key, long i, String value) {
		stringRedisTemplate.opsForList().remove(key, i, value);
	}

	/**
	 * retrieval
	 * 
	 * @param key
	 * @param index
	 * @return
	 */
	public String index(String key, long index) {
		return stringRedisTemplate.opsForList().index(key, index);
	}

	/**
	 * Set value
	 * 
	 * @param key
	 * @param index
	 * @param value
	 */
	public void set(String key, long index, String value) {
		stringRedisTemplate.opsForList().set(key, index, value);
	}

	/**
	 * Tailoring
	 * 
	 * @param key
	 * @param start
	 * @param end
	 */
	public void trim(String key, long start, int end) {
		stringRedisTemplate.opsForList().trim(key, start, end);
	}
}

 

 

The following instructions, such as LPUSH and RPUSH, are actually different commands pressing the stack from the left or from the right. You can think of the stack as an array from left to right. If you press the stack on the left and exit the stack on the right, that's the queue entry/exit operation; if you press the stack on the left and exit the stack on the left, that's the stack operation.

 

For example:

Queue operation: LPUSH enters the queue, RPOP enters the queue, similarly, L|R can be replaced.

Stack operation: LPUSH stack, LPOP out of the stack, the same, can replace L|R.

 

The following test cases are carried out. Initially and at the end of the test cases, the entry and exit operations are done respectively. During the test cases, stack and queue operations are carried out. No need to go into details. Look at the test cases. It's very simple!

 

/**
 * Mar 5, 2013
 */
package org.zlex.redis;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zlex.redis.support.ListOps;

/**
 * 
 * @author snowolf
 * @version 1.0
 * @since 1.0
 */
public class ListOpsTest {
	private ApplicationContext app;
	private ListOps listOps;
	private String key = "queue";

	@Before
	public void before() throws Exception {
		app = new ClassPathXmlApplicationContext("applicationContext.xml");
		listOps = (ListOps) app.getBean("listOps");

		System.out.println("------------IN---------------");
		for (int i = 0; i < 5; i++) {
			String uid = "u" + i;
			System.out.println(uid);
			listOps.in(key, uid);
		}
	}

	@After
	public void after() {
		// ------------OUT---------------
		System.out.println("------------OUT---------------");
		long length = listOps.length(key);
		for (long i = 0; i < length; i++) {
			String uid = listOps.out(key);
			System.out.println(uid);
		}
	}

	@Test
	public void stack() {
		// ------------PUSH---------------
		String key = "stack";
		int len = 5;
		System.out.println("------------PUSH---------------");
		for (int i = 0; i < len; i++) {
			String uid = "u" + System.currentTimeMillis();
			System.out.println(uid);
			listOps.push(key, uid);
		}

		long length = listOps.length(key);
		assertEquals(len, length);

		// ------------POP---------------
		System.out.println("------------POP---------------");
		for (long i = 0; i < length; i++) {
			String uid = listOps.pop(key);
			System.out.println(uid);
		}
	}

	@Test
	public void index() {

		// -------------INDEX-------------
		String value = listOps.index(key, 3);
		assertEquals("u3", value);
	}

	@Test
	public void range() {
		// -------------RANGE-------------
		List<String> list = listOps.range(key, 3, 5);
		boolean result1 = list.contains("u3");
		assertEquals(true, result1);

		boolean result2 = list.contains("u1");
		assertEquals(false, result2);
	}

	@Test
	public void trim() {
		// ------------TRIM---------------
		List<String> list = listOps.range(key, 3, 5);
		listOps.trim(key, 3, 5);
		boolean result3 = list.contains("u1");
		assertEquals(false, result3);
	}

	@Test
	public void set() {
		// ------------SET-----------------
		List<String> list = listOps.range(key, 3, 5);
		listOps.set(key, 4, "ux4");
		boolean result4 = list.contains("u4");
		assertEquals(true, result4);

	}

	@Test
	public void remove() {
		// ------------REMOVE-----------------
		listOps.remove(key, 4, "u4");
		String value = listOps.index(key, 4);
		assertEquals(null, value);

	}
}

 

Go back and tidy up. That's the right routine!

 

See the attachment for details!

 

Related links:

Conquer Redis

Conquer Redis + Jedis

Conquer Redis + Jedis + Spring (1) - Configuration & General Operation (GET SET DEL)

Conquer Redis + Jedis + Spring (2) - Hash Table Operation (HMGET HMSET)

Conquer Redis + Jedis + Spring (3) - List Operation

 

Reproduced in: https://my.oschina.net/mohaiyong/blog/221261

Topics: Redis Spring Jedis Junit