Redis 同步和执行
Redis SYNC and EXEC
Redis 管道有 2 个选项可以将命令发送到数据库 "exec" 和 "sync"。我想知道它们之间有什么区别。
我将使用 Jedis,java api 用于 redis,作为示例。
例子
示例 1
try (Jedis resource = redisManager.getResource()) {
Pipeline pipeline = resource.pipelined();
pipeline.multi();
pipeline.del("test-1");
pipeline.hset("test-2", "a", "b");
pipeline.exec();
}
示例 2
try (Jedis resource = redisManager.getResource()) {
Pipeline pipeline = resource.pipelined();
pipeline.multi();
pipeline.del("test-1");
pipeline.hset("test-2", "a", "b");
pipeline.sync();
}
管道执行方法returns
Response<List<?>>
而 sync returns void,但是,要使用 sync 从管道获取响应,您必须捕获单独的响应,类似这样。
Response<Long> isDeleted = pipeline.del("test-1");
Response<Long> isSuccess = pipeline.hset("test-2", "a", "b");
Response<List<String>> hvals = pipeline.hvals("test-2");
pipeline.sync();
assertEquals(1, (long) isDeleted.get());
Redis 管道有 2 个选项可以将命令发送到数据库 "exec" 和 "sync"。我想知道它们之间有什么区别。
我将使用 Jedis,java api 用于 redis,作为示例。
例子
示例 1
try (Jedis resource = redisManager.getResource()) {
Pipeline pipeline = resource.pipelined();
pipeline.multi();
pipeline.del("test-1");
pipeline.hset("test-2", "a", "b");
pipeline.exec();
}
示例 2
try (Jedis resource = redisManager.getResource()) {
Pipeline pipeline = resource.pipelined();
pipeline.multi();
pipeline.del("test-1");
pipeline.hset("test-2", "a", "b");
pipeline.sync();
}
管道执行方法returns
Response<List<?>>
而 sync returns void,但是,要使用 sync 从管道获取响应,您必须捕获单独的响应,类似这样。
Response<Long> isDeleted = pipeline.del("test-1");
Response<Long> isSuccess = pipeline.hset("test-2", "a", "b");
Response<List<String>> hvals = pipeline.hvals("test-2");
pipeline.sync();
assertEquals(1, (long) isDeleted.get());