Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

caching - Is it possible to create multiple cache stores using Spring's cache abstraction with redis?

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);

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.

But this works fine when I use Spring's SimpleCacheManager.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...