简体   繁体   English

如何使用部分密钥从二级ehcache中退出(通过注释)?

[英]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.

相关问题 从我的二级 ehcache 检索项目后获取“org.hibernate.LazyInitializationException”异常 - Getting “org.hibernate.LazyInitializationException” exceptions after retrieving items from my second-level ehcache 为什么我的实体不会从我的二级缓存中被逐出? - Why is my entity not evicted from my second-level cache? Ehcache与Hibernate和Spring异常集成:二级缓存未启用 - Ehcache integration with Hibernate & Spring Exception: Second-level cache is not enabled for usage 如何使用二级缓存检查JPA /休眠数据库是否已启动 - How to check if an JPA/hibernate database is up with second-level caching 试图逐出二级缓存 - Trying to evict second level cache 使用 Spring 以编程方式驱逐 Ehcache 元素 - Evict Ehcache elements programmatically, using Spring 驱逐EhCache中的所有元素 - Evict all element in EhCache 我应该如何在ehCache中使用@CachePut和@CacheEvict注释(ehCache 2.4.4,Spring 3.1.1) - How should I use @CachePut and @CacheEvict annotations with ehCache (ehCache 2.4.4, Spring 3.1.1) 如何使用注释在具有主键以外的列的 2 个实体之间加入? - How do i join between 2 entities with column other than primary key using annotations? Spring + Hibernate ehcache二级缓存 - Spring + Hibernate ehcache second level cache
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM