1.google了下有这种写法,执行报错
org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR EXEC without MULTI
public void setCacheList(String key, List<T> valueList) {
Assert.hasText(key, "key must not be null");
Assert.notEmpty(valueList, "valueMap must not be empty");
redisTemplate.setEnableTransactionSupport(true);//1
// redisTemplate.watch(key); // watch某个key,当该key被其它客户端改变时,则会中断当前的操作
redisTemplate.multi();//2
String standardKey = getStandardKey(key);
ListOperations<String, Object> opsForList = redisTemplate.opsForList();
valueList.stream().forEach(value -> {
opsForList.rightPush(standardKey, value );
});
redisTemplate.exec();//3
redisTemplate.setEnableTransactionSupport(false);
}
2、换成下面这种
public void setCacheList(String key, List<T> valueList) {
Assert.hasText(key,"key must not be null");
Assert.notEmpty(valueList,"valueMap must not be empty");
String standardKey = getStandardKey(key);
SessionCallback<List> sessionCallback = new SessionCallback<List>() {
@Override
public List execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.delete(standardKey);
ListOperations<String, Object> opsForList = redisTemplate.opsForList();
valueList.stream().forEach(value -> {
opsForList.rightPush(standardKey, value);
});
operations.expire(standardKey,SECONDS_OF_ONE_DAY, TimeUnit.MINUTES);
return operations.exec();
}
};
redisTemplate.execute(sessionCallback);
}
| 1 | DISCARD
取消事务,放弃执行事务块内的所有命令。 |
| 2 | EXEC
执行所有事务块内的命令。 |
| 3 | MULTI
标记一个事务块的开始。 |
| 4 | UNWATCH
取消 WATCH 命令对所有 key 的监视。 |
| 5 | WATCH key [key ...]
监视一个(或多个) key ,如果在事务执行之前这个(或这些) key 被其他命令所改动,那么事务将被打断。 |