我有一个问题,其中net.sf.ehcache.CacheManager
显示返回无效统计信息。我使用ehcache-core v2.3.2
(最新版本)与ehcache-spring-annotations
。不正确的ehcache统计信息:命中+未命中== 0
问题是getMemoryStoreObjectCount
返回对象同时兼具getCacheHits
和getCacheMisses
返回。是不是总数应该是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
}
为了完整性我包括所有必需的结构:
Spring配置
<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([email protected](name="StringCacheKeyGenerator"))
public Entity getEntity(Serializable key) {
return // sql ...
}
嗨,你介意发布你的代码,关于如何从Spring配置中导出JUnit测试中的“缓存”变量? – Dave 2016-02-19 16:42:39