为什么从mysql获取数据比spring cache + redis更快

时间:2018-11-05 08:24:25

标签: java spring-boot redis-cache

当我使用Spring缓存时,它需要直接访问数据库的时间是原来的两倍,但是如果我使用redistempla从Redis获取数据,则它比MySQL快。 所以我可以确保Redis中有数据,

此外,我刚刚测试了一个简单数据的查询,该项目使用带有Redis集群的Spring Boot和Spring Cloud

他是我的配置

@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;
}

0 个答案:

没有答案