最近在学习spring boot+redis有关的问题,但是在学习的时候莫名的遇到了一个问题:Cannot resolve constructor 'RedisCacheManager(org.springframework.data.redis.core.RedisTemplate)。这就很难受了问题如下图所示:
后来发现是版本问题,在springboot2.x中,RedisCacheManager已经没有了单参数的构造方法。我上边的方法是适用于spring 1x版本的。我的spring boot的版本是2x以上的版本。。。。。
直接改成下面管理缓存的放大就行了:
// 管理缓存
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}