spring-boot-starter-data-redis transaction list batch pop data

1. Dependent package

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
      <version>2.3.8.RELEASE</version>
</dependency>

        
2. Injection

@Autowired
private StringRedisTemplate redisTemplate;


    
3. Pop batch data

public List<String> leftPops(final String key, final int count) {
        final List<Object> txResults = this.redisTemplate.execute(new SessionCallback<List<Object>>() {
            @Override
            public List<Object> execute(final RedisOperations operations) throws DataAccessException {
                operations.multi();
                operations.opsForList().range(key, 0, count - 1);
                operations.opsForList().trim(key, count, -1);
                return operations.exec();
            }
        });
        if (CollectionUtils.isEmpty(txResults)) {
            return Collections.emptyList();
        }
        final Object object = txResults.get(0);
        if (object instanceof ArrayList) {
            //返回最终结果
            return (List<String>) object;
        }
        return Collections.emptyList();
}

 

Guess you like

Origin blog.csdn.net/qq_38428623/article/details/113745108