是否可以使用带有Redis的Spring缓存抽象来创建多个缓存存储区? [英] Is it possible to create multiple cache stores using Spring's cache abstraction with redis?
问题描述
我正在使用Spring MVC开发一个Web应用程序,并且正在使用带有Redis的spring缓存抽象来缓存我的数据库查询.但是我无法使用@Cacheable
创建多个缓存存储.
I'm developing a web application using Spring MVC and I'm using using spring's cache abstraction with Redis to cache my database queries. But I am not able to create multiple cache stores using @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
".即使我将这两种方法都缓存在不同的缓存中,它似乎也没有作用.
Using redis, both s
and r
have the same value "a
". Even though I cache the two methods in different caches, it seems to have no effect.
但是当我使用Spring的SimpleCacheManager
时,这可以很好地工作.
But this works fine when I use Spring's SimpleCacheManager
.
Redis的Spring bean配置:
Spring bean configuration for Redis:
<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>
推荐答案
根据文档,RedisCacheManager默认情况下直接保存键,而不附加前缀(缓存名称,用作名称空间).要更改它并避免冲突,请将'usePrefix'设置为'true':
According to the docs the RedisCacheManager by default saves the keys directly, without appending a prefix (cache name, which acts as a namespace). To change it and avoid clashes set 'usePrefix' to 'true': http://static.springsource.org/spring-data/data-redis/docs/current/api/org/springframework/data/redis/cache/RedisCacheManager.html
这篇关于是否可以使用带有Redis的Spring缓存抽象来创建多个缓存存储区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!