【发布时间】:2018-03-01 13:44:00
【问题描述】:
我想检索多个仅包含指定字段的 hashmap 值。所以我选择了 Redis 管道。
在测试以下代码时,我看到redisResponse1 始终为空,而redisResponse2 具有价值。
getRedisTemplate().executePipelined(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
List<byte[]> redisResponse1 = connection.hMGet(key.getBytes(), params);
List<byte[]> redisResponse2 = getRedisTemplate().getConnectionFactory().getConnection().hMGet(key.getBytes(), specificParams);
return null;
}
});
当我查看代码并发现下面的代码时,
a) redisResponse2 不使用管道选项执行
b) redisResponse1 使用管道 (isPipelined() == true) 执行,但始终返回 null。
public List<byte[]> hMGet(byte[] key, byte[]... fields) {
try {
if (isPipelined()) {
pipeline(new JedisResult(pipeline.hmget(key, fields)));
return null;
}
if (isQueueing()) {
transaction(new JedisResult(transaction.hmget(key, fields)));
return null;
}
return jedis.hmget(key, fields);
} catch (Exception ex) {
throw convertJedisAccessException(ex);
}
}
所以问题是
1) 如何使用管道选项实现我的用例?
2) 在这个 RedisCallback 中访问 getRedisTemplate().getConnectionFactory().getConnection() 有什么影响?
3)整个管道概念是如何运作的?它像动态 Lua 吗?这个 Java 代码在哪里转换为 Lua 脚本并作为脚本发送到 Redis,在 Redis 中执行并返回?在这个回调中感到惊讶;代码也在访问/更新外部类变量,那么所有这些变量会发生什么?所有这些外部类变量也发送到 lua 中的 redis?
4) 我看到很多关于doInRedis API 正在返回null 的例子;为什么这样?如何从中返回/获取有效对象?
【问题讨论】:
-
使用 spring-data-redis-1.6.6.RELEASE.jar
标签: java lua redis spring-data pipelining