[英]How do I evict (through annotations) from my second-level ehcache using a partial key?
I'm using Hibernate 4.3.11.Final with ehcache and Spring 3.2.11.RELEASE. 我正在将Hibernate 4.3.11.Final与ehcache和Spring 3.2.11.RELEASE一起使用。 I have the below Spring/ehcache configuration …
我有以下Spring / ehcache配置…
<cache:annotation-driven key-generator="cacheKeyGenerator" />
<bean id="cacheKeyGenerator" class="org.mainco.subco.myproject.util.CacheKeyGenerator" />
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="true" />
<util:map id="jpaPropertyMap">
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.dialect" value="org.mainco.subco.myproject.jpa.subcoMysql5Dialect" />
<entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
<entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
<entry key="hibernate.cache.use_second_level_cache" value="true" />
<entry key="hibernate.cache.use_query_cache" value="false" />
<entry key="hibernate.generate_statistics" value="true" />
<entry key="javax.persistence.sharedCache.mode" value="ENABLE_SELECTIVE" />
</util:map>
<bean id="sharedEntityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
using the below custom key generator … 使用下面的自定义密钥生成器...
public class CacheKeyGenerator implements KeyGenerator
{
@Override
public Object generate(final Object target, final Method method,
final Object... params) {
final List<Object> key = new ArrayList<Object>();
key.add(method.getDeclaringClass().getName());
key.add(method.getName());
for (final Object o : params) {
key.add(o);
}
return key;
}
}
As you can see, the keys are generated based on the class name, the method name, and then any parameters. 如您所见,密钥是根据类名称,方法名称以及所有参数生成的。 My question is, if I want to remove from my second-level cache all entries whose cache key's first entry (because my key is an array) is “org.mainco.subco.standards.repo.StandardsDao”, how would I write such a @CacheEvict rule?
我的问题是,如果我想从二级缓存中删除所有缓存键的第一个条目(因为我的键是一个数组)为“ org.mainco.subco.standards.repo.StandardsDao”的条目,我该怎么写? @CacheEvict规则? The below does not work …
以下不起作用…
@Caching(evict = { @CacheEvict(value="main", key="{'org.mainco.subco.standards.repo.StandardsDao'}")})
public int deleteCorrelationTypeContexts(String categoryId)
Any guidance is appreciated. 任何指导表示赞赏。 One constraint, it is not an option to use multiple second-level caches — I can only use one for this application (the one named “main”).
一个约束,使用多个第二级缓存不是一种选择—对于该应用程序,我只能使用一个(名为“ main”的缓存)。
The short answer is that this does not work. 简短的答案是,这不起作用。 That is not the way the
@CacheEvict
annotation works . 这不是
@CacheEvict
注释的工作方式 。
And also most cache implementations do not even have such an API natively. 而且,大多数缓存实现甚至本机都没有这样的API。
I had the very same problem and also found out that "This is not the way common cache implementations work". 我遇到了同样的问题,并且还发现“这不是通用缓存实现的工作方式”。
So you need to implement your own Cache Provider and override whatever "key's matcher" there is in the implementation of your choice. 因此,您需要实现自己的缓存提供程序,并覆盖您选择的实现中存在的任何“键匹配器”。
A good answer with GuavaCache examples can be found here . 在此处可以找到有关GuavaCache示例的良好答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.