SelfPopulatingCache 现有缓存问题(Spring 4,EhCache 2.10.3)
SelfPopulatingCache incumbent cache issue (Spring 4, EhCache 2.10.3)
我有一个项目设置 (这里的片段来自我在 GitHub https://github.com/ashishmarwal/self-populating-cache-issue 上创建的演示项目) 其中有一个原始的 eh-cache缓存在 ehcache 配置中声明 (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 存储库来演示这个问题:
https://github.com/ashishmarwal/self-populating-cache-issue
谢谢!!!
您在 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 将始终 return 相同 CacheManager
。在你的情况下(通常)你不希望那样。
只需将 shared
设置为 false
即可解决您的问题。
我有一个项目设置 (这里的片段来自我在 GitHub https://github.com/ashishmarwal/self-populating-cache-issue 上创建的演示项目) 其中有一个原始的 eh-cache缓存在 ehcache 配置中声明 (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 存储库来演示这个问题: https://github.com/ashishmarwal/self-populating-cache-issue
谢谢!!!
您在 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 将始终 return 相同 CacheManager
。在你的情况下(通常)你不希望那样。
只需将 shared
设置为 false
即可解决您的问题。