[英]Spring: hibernate + ehcache
我正在使用一個使用hibernate的spring項目,並期望使用ehcache實現二級緩存。 我看到了很多方法:
spring-modules-cache
引入了@Cacheable
注釋
ehcache-spring-annotations
一個工具集,旨在成為spring-modules-cache
的繼承者。
Hibernate cache
很好地集成到hibernate本身,以使用例如@Cache
注釋執行緩存。
使用代理的Programmatic cache
。 基於注釋的配置很快變得有限或復雜(例如,幾個級別的注釋嵌套)
我個人認為spring-modules-cache
不夠徹底,因此我可能更願意考慮更積極開發的ehcache-spring-annotations
。 Hibernate cache
雖然似乎是最完整的實現(例如,讀取和寫入緩存等)。
什么會激發使用哪個工具集? 請分享您的緩存體驗......
我們的項目使用選項3.我們將注釋org.hibernate.annotations.Cache
應用於我們在Ehcache中緩存的實體,使用ehcache.xml
配置Ehcache,並在hibernate.cfg.xml
啟用和配置Hibernate二級緩存 :
<!-- Enable the second-level cache -->
<property name="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</property>
<property name="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.EhCacheRegionFactory
</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_structured_entries">true</property>
<property name="hibernate.cache.generate_statistics">true</property>
對於大多數實體,我們使用緩存並發策略CacheConcurrencyStrategy.TRANSACTIONAL
:
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
我們的Maven項目使用Hibernate 3.3.2GA和Ehcache 2.2.0:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.2.1.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>3.3.2.Beta1</version>
</dependency>
Spring 3.1有一個新的內置緩存抽象。 在這里閱讀
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.