我有一个代码,在其中我实现了缓存机制。以前是基于番石榴的缓存,现在我转向Redis,考虑到集中式缓存的需求。
但我很担心它的表现,因为我看到与guave相比,redis的性能非常低。
我测量了一个api的性能,它从缓存中获取类对象--在Guava的情况下是5ms,而在Redis中是200 5ms。
这是在负载测试情况下的平均响应,在单个请求响应没有太大差异的情况下。
我用缓存抽象实现了Spring。
下面是示例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与番石榴相比性能很差?
通过更多的配置,可以将redis性能与番石榴的性能进行比较吗?
请建议一下。
发布于 2015-08-31 15:43:44
免责声明:我绝不是使用番石榴或红宝石的专家,尽管我两者都使用过。
明显的性能损失是显而易见的。
首先,在我看来,当你从番石榴切换到红宝石时,你会遇到性能下降,这是完全正常的。
主要是因为:
因此,即使您在同一台机器上,并且即使Redis固有的性能优于番石榴的缓存(老实说,一般情况下可能是这样),您也肯定会看到性能上的成功。
可能的改进
尽管如此,您可能可以通过配置和体系结构选择来提高性能:
https://stackoverflow.com/questions/32075954
复制相似问题