Home >Database >Redis >How to use opsForList().range() in redis

How to use opsForList().range() in redis

王林
王林forward
2023-05-26 13:46:201740browse

Conclusion (please read below for specific test data)

1. If start—end maintains the overall order, there will be no problem

2. Follow the order, even if start N-1 can also query data

3. Special usage: Through stringRedisTemplate.opsForList().range(key, 0, -1), you can query the first index to the last index ( That is, all data)

1. Environment redis

How to use opsForList().range() in redis

2. Test code:

  @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void testRedis() {
        String key = "testList";
        String[] data = new String[]{"1_1", "1-2", "2_1", "2_2"};
        Boolean flag = stringRedisTemplate.hasKey(key);
        if (!flag) {
            stringRedisTemplate.opsForList().leftPushAll(key, data);
        }
        List<String> range = stringRedisTemplate.opsForList().range(key, -100, -1);
        System.out.println("range = " + range);
    }

3. Test data (assuming the List length is N)

How to use opsForList().range() in redis

4. The test starts from the reciprocal index

4.1. stringRedisTemplate.opsForList().range(key, -4, -1)

From the results: the 4th from the last index - the 1st from the last index

How to use opsForList().range() in redis

4.2, stringRedisTemplate.opsForList().range(key, -3, -1)

From the results: the third from the last index - the first from the last index

How to use opsForList().range() in redis

4.3, stringRedisTemplate. opsForList().range(key, -3, -2)

From the results: the third from the last index - the second from the last index

How to use opsForList().range() in redis

4.4, stringRedisTemplate.opsForList().range(key, -2, -3)

From the results: the second from the last index - the third from the last index will not work, from The third from the bottom of the index - the second from the bottom of the index is fine.

Conclusion: Starting from the Nth index, it must be in order (i.e. -N, -(N-1), -(N-2),..., -1). Reverse order is not possible. ’

How to use opsForList().range() in redis

5. The test starts from the positive index number

5.1. stringRedisTemplate.opsForList().range(key, 0, 3)

From the results: From the first index - the fourth index

How to use opsForList().range() in redis

##5.2, stringRedisTemplate.opsForList().range(key, 1,2)

From the results: From the second index - the third index

How to use opsForList().range() in redis##5.3, stringRedisTemplate.opsForList().range(key, 2,1)

From the results: from the 3rd index - the 2nd index is not possible, from the 2nd index - the 3rd index is OK

Conclusion : Starting from index 0, it must be in order (i.e. 0, 1, 2,..., N-1), reverse order is not possible

How to use opsForList().range() in redis6. Test index Countdown——Positive index (positive index, subscript 0 is the first one)

6.1, stringRedisTemplate.opsForList().range(key, -2, 2)

Judging from the results: it is not difficult to understand, from the second to last index (that is, row is 3), to the third index (that is, row is 3)

How to use opsForList().range() in redis 6.2. stringRedisTemplate.opsForList().range(key, -4, 3)

From the results: it is not difficult to understand, from the fourth to last index (that is, row is 1), to the index 3 (that is, row is 4)

How to use opsForList().range() in redis6.3, stringRedisTemplate.opsForList().range(key, -4, 5)

From the results: From the 4th to last index (i.e., row is 1), to the 5th index (i.e., row is 6)

Conclusion: It is no problem to exceed in order

The above is the detailed content of How to use opsForList().range() in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete