私はSpring MVCを使用してWebアプリケーションを開発しており、RedisでSpringのキャッシュ抽象化を使用してデータベースクエリをキャッシュしています。しかし、を使用して複数のキャッシュ ストアを作成することはできません@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
" になります。2 つのメソッドを異なるキャッシュにキャッシュしても、効果がないようです。
しかし、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>