[14] springboot learn fast. Redis operation of the list

Foreword

Before explain the string data structure springboot (StringRedisTemplate) redis operation, and this article will explain the list data structure

list data structure having an operation

The map shows redis list data structure having methods

The method provides StringRedisTemplate

RedisTemplate redis list provided a method of operating as follows:

Below, we pick some commonly used as an example to explain the method, other methods can refer to the API.

range

Redis now has a list, which is a key: happyjava: list, and there are three values, as follows:

range (K key, long start, long end) method, is to obtain the specified range of the content list

subscript start from scratch. As follows, to obtain the second third of the element:

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

Output:

blog.happyjava.cn
​
www.happyjava.cn
复制代码

size(K key)

Obtain a list of size

@Test
public void testSize() {
 Long size = redisTemplate.opsForList().size("happyjava:list");
 System.out.println(size);
}
复制代码

Output:

leftPush(K key, V value)

The press-fitting method identification data from the left of the list (the upper, or head of the list). If the key does not exist, the key is created.

@Test
public void testLeftPush() {
 redisTemplate.opsForList().leftPush("happyjava:list", "new happyjava");
}
复制代码

After the execution, to view the data in redis:

leftPushAll

There are two methods overloaded, namely variable parameters: leftPushAll (K key, V ... values);,

Set parameters: leftPushAll (K key, Collection values)

Uncertain parameters:

@Test
public void testLeftPushAll() {
 redisTemplate.opsForList().leftPushAll("happyjava:list","apple","happy");
}
复制代码

After performing View data:

Set of parameters:

@Test
public void testLeftPushAll() {
// redisTemplate.opsForList().leftPushAll("happyjava:list","apple","happy");
 redisTemplate.opsForList().leftPushAll("happyjava:list", Arrays.asList("new happy1", "new happy2"));
}
复制代码

After performing View data:

rightPush(K key, V value)

Press-fitting the data from the end of the list

@Test
public void testRightPush() {
 redisTemplate.opsForList().rightPush("happyjava:list","java");
}
复制代码

After performing redis View data:

rightPushAll

This method leftPushAll class is overloaded as there are two methods, as follows:

Here is not to do too much introduced.

set(K key, long index, V value)方法

Can directly set a target value in the list, index starts at 0

@Test
public void testSet() {
 redisTemplate.opsForList().set("happyjava:list",1,"test set");
}
复制代码

After performing redis View

remove(K key, long count, Object value)方法

To delete a value, delete count one, delete from scratch

Now redis data are as follows:

Execute the code below:

@Test
public void testRemove() {
 redisTemplate.opsForList().remove("happyjava:list",2,"happy");
}
复制代码

After performing View data:

index(K key, long index)方法

Returns the index (starting from 0) under the underlying elements. Now redis data are as follows:

@Test
public void testIndex() {
 String value = redisTemplate.opsForList().index("happyjava:list", 1);
 System.out.println(value);
}
复制代码

Execution results are as follows:

leftPop

There are two overloaded methods:

V leftPop (K key);

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

This method will pop up a list of elements of the head (after the pop delete in the list), like the stack, the method overloading, if the list of data does not exist, blocking live, and so the data is returned (the longest timeout blocking time is set). The method corresponds to the operation is redis BLPOP (blocking left pop)

rightPop

And leftPop similar, but the pop-up list data from the tail

to sum up

This article explains the list data structure springboot redis operation, most of the method has been done to explain and demonstrate. redis list is also very commonly used data structures, the future will continue to explain redis other data structures.

Reproduced in: https: //juejin.im/post/5d05e7cee51d455d88219ef4

Guess you like

Origin blog.csdn.net/weixin_34096182/article/details/93183802