preface

Springboot (StringRedisTemplate) operates on the String data structure of Redis. This article will cover the list data structure

The operations that the list data structure has

The following figure lists the methods that the Redis List data structure has

StringRedisTemplate provides the method

The RedisTemplate provides the following list of methods for manipulating redis:

Below, we pick some common methods as examples to explain, other methods, can refer to the API description.

range

Redis now has a list with the key happyjava:list and three values as follows: happyjava:list

Range (K key, long start, long end)

Start subscripts start from zero. Get the second to third elements as follows:

@Test
public void testList() {
 List<String> list = redisTemplate.opsForList().range("happyjava:list", 1, 2); assert list ! = null; list.forEach(System.out::println); }Copy the code

Output result:

blog.happyjava.cn
​
www.happyjava.cn
Copy the code

size(K key)

Get the size of the list

@Test
public void testSize() {
 Long size = redisTemplate.opsForList().size("happyjava:list");
 System.out.println(size);
}
Copy the code

Output result:

leftPush(K key, V value)

This method identifies pushing data from the left side of the list (the top, or head of the list). If the key does not exist, it is created.

@Test
public void testLeftPush() {
 redisTemplate.opsForList().leftPush("happyjava:list"."new happyjava");
}
Copy the code

After executing, go to the data in Redis:

leftPushAll

LeftPushAll (K key, V… values); .

Set parameters: leftPushAll(K key, Collection Values)

Indeterminate parameter:

@Test
public void testLeftPushAll() {
 redisTemplate.opsForList().leftPushAll("happyjava:list"."apple"."happy");
}
Copy the code

View data after execution:

Set parameters:

@Test
public void testLeftPushAll() {
// redisTemplate.opsForList().leftPushAll("happyjava:list"."apple"."happy");
 redisTemplate.opsForList().leftPushAll("happyjava:list", Arrays.asList("new happy1"."new happy2"));
}
Copy the code

View data after execution:

rightPush(K key, V value)

Push data from the end of the list

@Test
public void testRightPush() {
 redisTemplate.opsForList().rightPush("happyjava:list"."java");
}
Copy the code

View redis data after executing:

rightPushAll

This method, like the leftPushAll class, has two overloaded methods as follows:

I won’t go into too much detail here.

Set (K key, long index, V value) method

You can directly set the value of a subscript in the list, index starting at 0

@Test
public void testSet() {
 redisTemplate.opsForList().set("happyjava:list", 1,"test set");
}
Copy the code

Check redis after execution

Remove (K key, long count, Object value) method

Delete a value, delete count, delete from the beginning

Now the data in Redis is as follows:

Execute the code:

@Test
public void testRemove() {
 redisTemplate.opsForList().remove("happyjava:list", 2,"happy");
}
Copy the code

View data after execution:

Index (K key, long index) method

Returns the element indexed with index (starting at 0). Now redis data is as follows:

@Test
public void testIndex() {
 String value = redisTemplate.opsForList().index("happyjava:list", 1);
 System.out.println(value);
}
Copy the code

The result is as follows:

leftPop

There are two overloaded methods:

V leftPop(K key);

V leftPop(K key, long timeout, TimeUnit unit);

This method pops the element in the header of the list (and then deletes it from the list), just like a stack. Overloaded methods block if there is no data in the list and return when there is data (the maximum block time is the timeout set). The redis method corresponds to BLPOP (blocking left pop)

rightPop

Similar to leftPop, except that data is popped from the end of the list

conclusion

This article explains springboot’s manipulation of redis’ list data structure, most of which has been explained and demonstrated. List is also a very common data structure in Redis, and we’ll cover other redis data structures later.