7

私は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 を使用すると、srの両方が同じ値 " 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>
4

1 に答える 1

8

ドキュメントによると、RedisCacheManager はデフォルトでキーを直接保存し、プレフィックス (名前空間として機能するキャッシュ名) を追加しません。これを変更して衝突を回避するには、「usePrefix」を「true」に設定します: http://static.springsource.org/spring-data/data-redis/docs/current/api/org/springframework/data/redis/cache/RedisCacheManager .html

于 2013-08-09T12:22:38.033 に答える