使用Spring的缓存抽象和Redis,是否可以创建多个缓存存储?

7

我正在使用Spring MVC开发Web应用程序,并使用Spring的Redis缓存抽象来缓存我的数据库查询。但是我无法使用@Cacheable创建多个缓存存储。

@Cacheable("acache")
public String atest(int i) {
   return "a";
}

@Cacheable("bcache")
public String btest(int i) {
   return "b";
}

...
...
String s = atest(1);
String r = btest(1);

使用Redis,sr的值都是"a"。即使我在不同的缓存中缓存了这两种方法,似乎也没有影响。

但是当我使用Spring的SimpleCacheManager时,这个问题就解决了。

Redis的Spring Bean配置:

<cache:annotation-driven />

<bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:hostName="${redis.host-name}"
        p:port="${redis.port}"
        p:usePool="true"/>


<bean id="redisTemplate"
        class="org.springframework.data.redis.core.RedisTemplate"
        p:connectionFactory-ref="jedisConnectionFactory"/>

<bean id="cacheManager"
        class="org.springframework.data.redis.cache.RedisCacheManager"
        c:template-ref="redisTemplate">
</bean>

你能展示一下你的Spring beans配置吗? - ragnor
添加了Beans配置。 - Rohan Laishram
1个回答

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接