我有一些問題讓hibernate二級緩存工作緩存域對象。根據ehcache documentation
,將緩存添加到我現有的工作應用程序中應該不會太複雜。春季休眠ehcache設置
我有以下設置(只有相關的片段進行了概述):
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE
public void Entity {
// ...
}
的Ehcache-entity.xml
<cache name="com.company.Entity" eternal="false"
maxElementsInMemory="10000" overflowToDisk="true" diskPersistent="false"
timeToIdleSeconds="0" timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
的applicationContext.xml
<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="annotatedClasses">
<list>
<value>com.company.Entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="net.sf.ehcache.configurationResourceName">/ehcache-entity.xml</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory</prop>
....
</property>
</bean>
Maven依賴
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
<version>2.0.8</version>
<exclusions>
<exclusion>
<artifactId>hibernate</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.3.2</version>
</dependency>
個
測試類是用來使緩存統計信息:
Cache cache = cacheManager.getCache("com.company.Entity");
cache.setStatisticsAccuracy(Statistics.STATISTICS_ACCURACY_GUARANTEED);
cache.setStatisticsEnabled(true);
// store, read etc ...
cache.getStatistics().getMemoryStoreObjectCount(); // returns 0
無操作似乎觸發任何緩存的變化。我錯過了什麼?目前我在DAO中使用HibernateTemplate
,這可能會有一些影響。
[編輯]
當設置爲DEBUG唯一的Ehcache日誌輸出是:
SettingsFactory: Cache region factory : net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
是否日誌告訴你什麼時候加載XML ecache? – heldt 2011-03-19 20:40:38
@heldt,我已更新我的帖子,以包含ehcache警告 – 2011-03-19 20:44:23