【发布时间】:2018-11-29 16:17:30
【问题描述】:
目前,我通过将@Cachable 与 Ehcache 一起使用来使用 Spring 缓存。我即将使用 Spring Data Redis 2.0.3 将 Ehcache 替换为 Redis。我在网上看到的所有示例都是基于旧版本的,但新版本的构造函数格式不同。
这是我当前的 cacheManager 配置:
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
基于老版本使用Redis的例子是:
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
c:template-ref="redisTemplate" />
新版本的构造函数与旧版本完全不同,新版本的所有示例都像这样手动将所有内容放入缓存中:
redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
我仍然想使用 cacheManager 和 @cachable 但不知道如何使用新版本的 Spring Data Redis 配置 cacheManager bean。新版本的构造函数需要RedisCacheWriter:
public RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration)
如果您能分享有关如何设置 cacheManager 以将 @Cachable 与新版本的 Spring Data Redis(最低 2.0.3)一起使用的想法,我将不胜感激。
【问题讨论】: