GP0*_*007 5 spring caching redis guava spring-cache
我有一个代码,我已经实现了缓存机制.以前它是基于番石榴的缓存,现在我正在转向Redis考虑集中缓存的需求.
但是我担心它的性能,因为与guave相比,我看到redis的性能极低.
我已经测量了api的性能,它从缓存获取一个类对象.在Guava的情况下它是5ms,而在Redis中它获得200ms.
这是负载测试情况下的平均响应,如果单个请求响应没有太大差异.
我已经使用缓存抽象实现了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;
}
}Run Code Online (Sandbox Code Playgroud)
除此之外,对于redis服务器配置,我已经尝试禁用所有持久性,因为我不需要它.但仍然表现不佳.
我的主要问题是,与Guava相比,导致此问题的配置还是Redis的性能非常低?
可以通过更多的配置调整redis性能与番石榴的性能进行比较吗?
请建议.
免责声明:尽管我使用过Guava或Redis,但我绝不是专家。
对于初学者来说,在我看来,从番石榴切换到Redis时性能下降是完全正常的。
主要是因为:
Guava提供的内存是内存中的,并且对于应用程序正在运行的JVM是本地的。因此,您的应用程序可以轻松查询,而无需借助任何进程间通信。
Redis是一个单独的键值存储应用程序,在其自己的进程中运行。结果,您必须以某种方式与它进行通信以建立连接并发送请求。
因此,即使您在同一台机器上,并且即使Redis的固有性能优于Guava的缓存(老实说,一般情况下也可能是这种情况),您肯定会看到性能下降。
话虽如此,您可能可以通过配置和体系结构选择来提高性能:
确保使用本地IP连接到Redis。这将有助于避免在尝试建立连接时解析任何地址。
确保通过尽可能轻量的协议连接到Redis。因为我假设您使用的是本地Redis服务器,并且遵守上一点,所以您不需要任何风吹草动,安全的协议等。
其他适用于您的方案的常规Redis配置调整。