本文探讨了在Spring Data Redis中使用Pipeline模式时的重要注意事项。由于Pipeline模式返回的是List<Object>类型,其结果会被后续的pipeline操作覆盖,因此强调了回调函数必须返回null,以避免数据覆盖的问题。文章深入解析了这一机制的工作原理,并提供了代码示例。
Note that the value returned from the RedisCallback is required to be null, as this value is discarded in favor of returning the results of the pipelined commands.
return execute(new RedisCallback<List<Object>>() {
public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
boolean pipelinedClosed = false;
try {
Object result = executeSession(session);
if (result != null) {
throw new InvalidDataAccessApiUsageException(
"Callback cannot return a non-null value as it gets overwritten by the pipeline");
}
List<Object> closePipeline = connection.closePipeline();
pipelinedClosed = true;
return deserializeMixedResults(closePipeline, resultSerializer, hashKeySerializer, hashValueSerializer);
} finally {
if (!pipelinedClosed) {
connection.closePipeline();
}
}
}
});