Is it possible to create multiple cache stores using Spring cache abstraction using redis?

I am developing a web application using Spring MVC and am using Spring cache abstraction with Redis to cache database queries. But I cannot create multiple cache repositories 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 << 24>. Although I cache two methods in different caches, this does not seem to have an effect.

But this works fine when I use Spring 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> 
+7
spring spring-mvc caching redis
source share
1 answer

According to the docs, RedisCacheManager by default saves keys directly, without adding a prefix (a cache name that acts as a namespace). To change it and avoid collisions, set 'usePrefix' to 'true': http://static.springsource.org/spring-data/data-redis/docs/current/api/org/springframework/data/redis/cache/ RedisCacheManager.html

+6
source share

All Articles