Spring Data Redis:Redis管道始终返回null [英] Spring Data Redis: Redis Pipeline returning always null
问题描述
我想只使用指定的字段来检索多个hashmap值.所以我选择了Redis管道.
在测试以下代码时,我看到redisResponse1
始终为null,因为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中找到 解决方案
The majority of your questions are available within the Spring Data Redis reference documentation.
Before digging into Pipelining, a single multi-get from one Hash does not require Pipelining because it's only a single command. Pipelining won't improve performance/stability/… of your Redis interaction.
Pipelining is arranged as callback and intended to issue multiple commands without awaiting the result immediately – think of it as a batch where you get all results later. Because pipelining synchronizes responses at the very end, you don't receive result values within the callback but at the very end, when the pipelining session is synchronized and executePipelined(…)
terminates.
Your code should rather look like:
List<Object> results = getRedisTemplate().executePipelined(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) {
connection.hMGet(key.getBytes(), params);
return null;
}
});
List<Object> hmget = (List<Object>) results.get(0);
You have to use only the connection that you receive as callback argument because the connection has entered pipelining mode. Obtaining a connection from outside the callback (like template.getConnectionFactory().getConnection()
) will open a new connection and execute Redis commands with awaiting responses – no pipelining is applied to any external obtained connection.
You can also use methods of RedisTemplate
instead of working with the plain connection. executePipelined(…)
binds the connection used in the callback to the current Thread and reuses that bound connection if you call template API methods.
Regarding your Lua question: The code/method calls are not transposed to Lua.
这篇关于Spring Data Redis:Redis管道始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!