我正在使用Spring MVC开发一个Web应用程序,我正在使用Spring的缓存抽象与Redis来缓存我的数据库查询.但我无法使用创建多个缓存存储@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);
Run Code Online (Sandbox Code Playgroud)
使用Redis的,既s与r具有相同的价值" a".即使我将这两种方法缓存在不同的缓存中,它似乎也没有效果.
但是当我使用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>
Run Code Online (Sandbox Code Playgroud) 我正在使用@cacheable注释来缓存函数的结果.我有3个不同的缓存,每个缓存的关键是当前登录用户的用户ID与方法中的参数连接.在某个事件中,我想要驱逐所有具有以该特定用户ID开头的密钥的缓存条目.例如 :
@Cacheable(value = "testCache1", key = "'abcdef'")
Run Code Online (Sandbox Code Playgroud)
我想缓存evict注释是这样的:
@CacheEvict(value = "getSimilarVendors", condition = "key.startsWith('abc')")
Run Code Online (Sandbox Code Playgroud)
但是当我尝试实现它时,它给了我一个错误:
Property or field 'key' cannot be found on object of type'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public?
Run Code Online (Sandbox Code Playgroud)
实现这个的正确方法是什么?