当我使用 Spring 缓存时,它需要的时间是直接访问数据库的两倍多,但如果我使用redistempla
从 Redis 获取数据,它比 MySQL 快。所以我可以确保 Redis 中有数据,
另外,我只是测试了一个简单数据的查询,项目使用Spring Boot和Spring Cloud搭配Redis集群
他是我的配置
@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;
}