SelfPopulatingCache现任缓存问题(Spring 4,EhCache 2.10.3)

时间:2017-04-22 04:27:59

标签: java spring caching ehcache

我有一个项目设置(这里的片段来自我在GitHub上创建的演示项目https://github.com/ashishmarwal/self-populating-cache-issue,其中在ehcache配置中声明了原始eh-cache缓存(ehcache .XML)。

<cache name="alphabet-description-cache"
       eternal="false"
       maxElementsInMemory="1000"
       memoryStoreEvictionPolicy="LRU"
       overflowToDisk="false"
       timeToLiveSeconds="300"
       timeToIdleSeconds="300" />
然后,Spring bean描述符使用该原始缓存使用CacheEntryFactory创建一个装饰(SelfPopulatingCache):                                              

<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="cacheManager"/>
</bean>

<!--Creating a decorated cache instance using the raw cache cinfigured in ehcache.xml -->
<bean id="alphabetDescriptionCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
    <property name="cacheManager" ref="cacheManager"/>
    <property name="cacheName" value="alphabet-description-cache"/>
    <property name="cacheEntryFactory" ref="alphabetDescriptionCacheEntryFactory"/>
</bean>

<bean id="alphabetDescriptionCacheEntryFactory" class="com.marwals.ashish.issues.selfpopulatingcache.AlphabetDescriptionCacheEntryFactory" />

我们还有一个test-context.xml,用于单元测试,并声明一个cacheManager以及装饰缓存(在我看来,我给这些缓存管理器指定了不同的名称):

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="cacheManagerName" value="self-populating-cache-issue-demo-test"/>
    <property name="shared" value="true"/>
    <property name="acceptExisting" value="false"/>
    <property name="configLocation" value="classpath:/ehcache.xml"/>
</bean>

<bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="cacheManager"/>
</bean>

<bean id="alphabetDescriptionCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
    <property name="cacheManager" ref="cacheManager"/>
    <property name="cacheName" value="alphabet-description-cache"/>
    <property name="cacheEntryFactory" ref="alphabetDescriptionCacheEntryFactory"/>
</bean>

<bean id="alphabetDescriptionCacheEntryFactory" class="com.marwals.ashish.issues.selfpopulatingcache.AlphabetDescriptionCacheEntryFactory" />

这里的问题是,如果我有两个不同的测试,每个加载主要或测试上下文bean描述符,我遇到一个现任缓存问题:

    Error creating bean with name 'alphabetDescriptionCache' defined in class path resource [test-context.xml]: Invocation of init method failed; 
nested exception is net.sf.ehcache.CacheException: Cannot replace alphabet-description-cache It does not equal the incumbent cache.

任何想法在这里可能是错的?调试代码显示我有两个不同的缓存实例用于相同的原始缓存,然后由EhCache的缓存管理器将其作为错误引发。

我创建了一个git repo来演示这个问题: https://github.com/ashishmarwal/self-populating-cache-issue

感谢!!!

1 个答案:

答案 0 :(得分:0)

您在Spring配置中明确请求了共享缓存管理器

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="cacheManagerName" value="self-populating-cache-issue-demo"/>
    <property name="shared" value="true"/> <!-- here -->
    <property name="acceptExisting" value="false"/>
    <property name="configLocation" value="classpath:/ehcache.xml"/>
</bean>

这意味着对于给定的配置,Ehcache将始终返回相同的CacheManager。在你的情况下(通常),你不希望这样。

只需将shared设置为false即可解决您的问题。