我正在尝试优化从 Redis 获取数据的性能。该服务器当前在我的 2015 Macbook Pro 上本地运行。
第一:问题说明 目前我只有 32 个 key 存储为散列。其中 16 个在每个散列值中存储相当长的 JSON 字符串,每个散列中有 <300 个字段。其余的都非常小,所以我对它们没有任何问题。
在一个 Spring Boot 应用程序中,使用 Spring Data Redis 模板,通过 Jedis 连接,通过管道传输 4 个 HGETALL 命令 4 次,检索 16 个大哈希的总时间约为 1700 毫秒。
我的问题: 我如何着手寻找真正的瓶颈?我已经检查了 SLOWLOG,它告诉我在服务器上执行的操作非常快,每个 HGETALL 命令 < 1 毫秒,这是可以预料的。这意味着瓶颈必须在 Java 应用程序和 Redis 服务器之间。延迟是否可能是另一个 ~1650ms 的原因? (延迟似乎不是其他较小哈希的问题。)或者它可能是我的 JSON 字符串的反序列化?我不确定如何测试它,因为我无法在 RedisTemplate 代码中放入计时器。
下面是我用来流水线化 HGETALL 命令的代码:
private Map<Date, Map<Integer, Departure>> pipelineMethod(List<String> keys, String keyspace) {
long pipeTime = System.currentTimeMillis();
List<Object> results = redisTemplate.executePipelined(
(RedisCallback) (connection) -> {
for (String key : keys) {
long actionTime = System.currentTimeMillis();
connection.hGetAll((keyspace + key).getBytes());
System.out.println("HGETALL finished in " + (System.currentTimeMillis()-actionTime) + "ms");
}
return null;
}
);
System.out.println("Pipeline finished in " + (System.currentTimeMillis()-pipeTime) + "ms");
Map<Date, Map<Integer, Departure>> resultMap = new ConcurrentHashMap<>();
for (int i = 0; i < keys.size(); i++) {
if (results.get(i) == null) {
resultMap.put(new Date(Long.parseLong(keys.get(i))), null);
log.debug("Hash map from redis on " + new Date(Long.parseLong(keys.get(i))) + " was null on retrieval");
}
else
resultMap.put(new Date(Long.parseLong(keys.get(i))), (Map<Integer, Departure>) results.get(i));
}
return resultMap;
}
如有任何建议,我们将不胜感激。
最佳答案
尝试使用带连接池的生菜客户端
private GenericObjectPoolConfig getPoolConfig() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
//All below should use the propertysources;
poolConfig.setMaxTotal(20);
poolConfig.setMaxIdle(20);
poolConfig.setMinIdle(0);
return poolConfig;
}
@Bean
@Primary
public RedisConnectionFactory redisConnectionFactory() {
DefaultLettucePool lettucePool = new DefaultLettucePool(redisHost, Integer.valueOf(redisPort).intValue(), getPoolConfig());
lettucePool.setPassword(redisPass);
lettucePool.afterPropertiesSet();
LettuceConnectionFactory clientConfig = new LettuceConnectionFactory(lettucePool);
clientConfig.afterPropertiesSet();
return clientConfig;
}
@Bean
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
return template;
}
关于java - 如何使用 Spring Data Redis 和本地 Redis 服务器查找性能瓶颈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39472333/