我正在使用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,s
和r
的值都是"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>