我有一个代码,我在其中实现了缓存机制。以前它是基于 Guava 的缓存,现在考虑到集中式缓存的需求,我正在转向 Redis。
但我担心它的性能,因为与 guave 相比,我看到 redis 的性能极低。
我已经测量了从缓存中获取类对象的 api 的性能在 Guava 的情况下为 5 毫秒,而在 Redis 中则为 200 毫秒。
这是负载测试情况下的平均响应,在单个请求响应的情况下差别不大。
我已经使用缓存抽象实现了 Spring 数据 Redis。
以下是 Redis 配置示例:
@Bean
public RedisConnectionFactory redisConnectionFactory(@Value("${redis.host}") String redisHost,
@Value("${redis.port}") Integer redisPort) {
JedisConnectionFactory cf = new JedisConnectionFactory();
cf.setHostName(redisHost);
cf.setPort(redisPort);
cf.setUsePool(true);
JedisPoolConfig jedisPool = new JedisPoolConfig();
jedisPool.setMaxTotal(500);
cf.setPoolConfig(jedisPool);
return cf;
}
@Bean(name = "redisTemplate")
RedisTemplate<Object,Object> redisTemplate() {
final RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
template.setConnectionFactory(applicationContext.getBean(RedisConnectionFactory.class));
return template;
}
@Bean
public CacheManager cacheManager() {
if(isRedisEnabled)
{
RedisTemplate<?,?> template = (RedisTemplate<?, ?>) applicationContext.getBean("redisTemplate");
RedisCacheManager redisCacheManager = new PieRedisCacheManager(template);
redisCacheManager.setUsePrefix(true);
try
{
template.getConnectionFactory().getConnection();
}
catch(Exception e)
{
LOG.error("Unable to connect to redis Server ,closing application : "+e);
SpringApplication.exit(applicationContext);
}
return redisCacheManager;
}
else
{
GuavaCacheManager guavaCacheManager = new GuavaCacheManager();
guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder());
return guavaCacheManager;
}
}
除此之外,对于 redis 服务器配置,我尝试禁用所有持久性,因为我不需要它。但仍然表现不佳。
我的主要疑问是导致此问题的配置还是 Redis 与 Guava 相比性能非常低?
通过更多的配置调优redis的性能可以和guava相比吗?
请建议。