此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Data Redis 3.5.3spring-doc.cadn.net.cn

流水线

Redis 提供对流水线的支持,它涉及向服务器发送多个命令,而无需等待回复,然后在单个步骤中读取回复。当您需要连续发送多个命令时,例如将许多元素添加到同一个列表时,流水线可以提高性能。spring-doc.cadn.net.cn

Spring Data Redis 提供了几个RedisTemplate在管道中运行命令的方法。如果您不关心流水线作的结果,则可以使用标准execute方法, 传递true对于pipeline论点。这executePipelined方法运行提供的RedisCallbackSessionCallback并返回结果,如以下示例所示:spring-doc.cadn.net.cn

//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允许您为管道结果传递自定义序列化程序的方法。spring-doc.cadn.net.cn

请注意,从RedisCallback被要求是null,因为此值将被丢弃,以返回流水线命令的结果。spring-doc.cadn.net.cn

Lettuce 驱动程序支持细粒度刷新控制,允许在命令出现时刷新命令、缓冲或在连接关闭时发送命令。spring-doc.cadn.net.cn

LettuceConnectionFactory factory = // ...
factory.setPipeliningFlushPolicy(PipeliningFlushPolicy.buffered(3)); (1)
1 在本地缓冲并在每 3 个命令后刷新。
流水线仅限于 Redis 独立版。 Redis Cluster 目前仅通过 Lettuce 驱动程序支持,但使用跨槽键时的以下命令除外:rename,renameNX,sort,bLPop,bRPop,rPopLPush,bRPopLPush,info,sMove,sInter,sInterStore,sUnion,sUnionStore,sDiff,sDiffStore. 完全支持同槽键。