我有一个代码,其中实现了缓存机制。
以前它是基于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 比较吗?
请提出建议。
最佳答案
免责声明:尽管我使用过Guava或Redis,但我绝不是专家。
明显的性能损失是明显的
对于初学者,在我看来,当您从Guava切换到Redis时,遇到性能下降是完全正常的。
主要是因为:
因此,即使您使用的是同一台计算机,并且即使Redis的固有性能优于Guava的缓存(老实说,一般情况下也可能是这种情况),您肯定会看到性能受到影响。
可能的改进
话虽如此,您可能可以通过配置和体系结构选择来提高性能:
关于spring - Redis vs Guava缓存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32075954/