【发布时间】:2011-07-12 22:30:29
【问题描述】:
我遇到net.sf.ehcache.CacheManager 出现返回无效统计信息的问题。
我正在使用ehcache-core v2.3.2(最新版本)和ehcache-spring-annotations。
问题是getMemoryStoreObjectCount返回1对象,而getCacheHits和getCacheMisses都返回0。总计数不应该是 hits + misses 吗?
下面的单元测试应该能说明问题(它应用于一个空数据库):
@Test
public void testCache() {
Entity e = ..
dao.storeEntity(e);
dao.getEntity(e);
assertEquals(1, cache.getStatistics().getMemoryStoreObjectCount()); // ok
assertEquals(0, cache.getStatistics().getCacheHits()); // ok
assertEquals(1, cache.getStatistics().getCacheMisses()); // fails due to 0
}
为了完整起见,我包括了所有必要的配置:
弹簧配置
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
ehcache.xml
<ehcache>
<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
道
@Cacheable(keyGenerator=@KeyGenerator(name="StringCacheKeyGenerator"))
public Entity getEntity(Serializable key) {
return // sql ...
}
【问题讨论】:
-
嗨,您是否介意发布您的代码,说明您是如何从 Spring 配置中派生 JUnit 测试中的“缓存”变量的?
标签: java hibernate spring annotations ehcache