1

When I use Spring cache it takes more than twice as long as direct access to the database, but if I use redistempla to get data from Redis, it is faster than MySQL. so I can make sure that there is data in Redis ,

In addition, I just tested the query of a simple data,the project use Spring Boot and Spring Cloud with Redis cluster

his is my configuration

@Bean
public CacheManager cacheManager(RedisTemplate ObjectRedisTemplate) {
    RedisCacheManager cacheManager = new RedisCacheManager(ObjectRedisTemplate);
    cacheManager.setUsePrefix(true);
    // Number of seconds before expiration. Defaults to unlimited (0)
    cacheManager.setDefaultExpiration(300L);
    return cacheManager;
}

@Bean(value="ObjectRedisTemplate")
public RedisTemplate<String, Object> redisTemplates(JedisConnectionFactory jedisConnectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    return redisTemplate;
}
zero323
  • 322,348
  • 103
  • 959
  • 935
teng
  • 19
  • 4
  • 2
    Sounds like something is misconfigured but without more details, it would be hard to say what. – Peter Lawrey Nov 05 '18 at 08:32
  • I have added more details on it , Could you check it out for me again,thx – teng Nov 05 '18 at 09:11
  • Is your redis server running on another hosts that has larger latency than the MySQL server ? – nos Nov 05 '18 at 09:15
  • I guess not, because I get the data directly from “redistemplate ” faster than mysql, – teng Nov 05 '18 at 09:22
  • You haven't described your problems clearly, there are many possibilities as I can see. 1. The cache has no data, it got data from database, so it became slowly. 2. You can examine your project, if your project structure has cache from database And more, it would be quickly when database has a few concurrence, you should test it when database become slow – user8917247 Nov 05 '18 at 09:22

0 Answers0