对于最新的稳定版本,请使用 Spring Data Redis 3.5.3! |
流水线
Redis 提供对流水线的支持,它涉及向服务器发送多个命令,而无需等待回复,然后在单个步骤中读取回复。当您需要连续发送多个命令时,例如将许多元素添加到同一个列表时,流水线可以提高性能。
Spring Data Redis 提供了几个RedisTemplate
在管道中运行命令的方法。如果您不关心流水线作的结果,则可以使用标准execute
方法, 传递true
对于pipeline
论点。这executePipelined
方法运行提供的RedisCallback
或SessionCallback
并返回结果,如以下示例所示:
//pop a specified number of items from a queue
List<Object> results = stringRedisTemplate.executePipelined(
new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
for(int i=0; i< batchSize; i++) {
stringRedisConn.rPop("myqueue");
}
return null;
}
});
前面的示例从管道中的队列中运行大量向右弹出项。
这results
List
包含所有弹出的项目。RedisTemplate
在返回之前使用其值、哈希键和哈希值序列化程序来反序列化所有结果,因此前面示例中返回的项是字符串。
还有其他executePipelined
允许您为管道结果传递自定义序列化程序的方法。
请注意,从RedisCallback
被要求是null
,因为此值将被丢弃,以返回流水线命令的结果。
Lettuce 驱动程序支持细粒度刷新控制,允许在命令出现时刷新命令、缓冲或在连接关闭时发送命令。
|
流水线仅限于 Redis 独立版。
Redis Cluster 目前仅通过 Lettuce 驱动程序支持,但使用跨槽键时的以下命令除外:rename ,renameNX ,sort ,bLPop ,bRPop ,rPopLPush ,bRPopLPush ,info ,sMove ,sInter ,sInterStore ,sUnion ,sUnionStore ,sDiff ,sDiffStore .
完全支持同槽键。 |