Spring:hibernate + ehcache [英] Spring: hibernate + ehcache
问题描述
我正在使用hibernate开发一个spring项目,并使用ehcache来实现二级缓存。我看到了很多方法:
-
spring-modules-cache
,它引入了@Cacheable
注释 .google.com / p / ehcache-spring-annotations /rel =noreferrer> -
Hibernate缓存
很好地集成到hibernate本身中来执行缓存,例如使用@Cache
注释。 spring-modules-cache
which introduces the@Cacheable
annotationehcache-spring-annotations
a toolset which aims to be the successor ofspring-modules-cache
.Hibernate cache
is nicely integrated into hibernate itself to perform caching using e.g., the@Cache
annotation.Programmatic cache
using proxies. Annotation based config quickly becomes either limited or complex (e.g., several levels of annotation nesting)
ehcache-spring-annotations
一个旨在成为继任者的工具集 spring-modules-cache
。 编程缓存
使用代理。基于注解的配置很快就会变得有限或者复杂(例如,多层次的注释嵌套)。我个人不会认为 spring-modules-cache
已经足够透彻,因此我可能更愿意考虑更积极地开发 ehcache-spring-annotations
。尽管Hibernate缓存似乎是最完整的实现(例如,读写缓存等)。
什么会激发哪个工具集使用?请分享您的缓存体验......我们的项目使用选项3.我们应用注释 org。
hibernate.cfg.xml
: < ! - 启用二级缓存 - >
< 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
: p>
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
我们的Maven项目使用Hibernate 3.3.2GA和Ehcache 2.2.0:
<依赖性>
< groupId> net.sf.ehcache< / groupId>
< artifactId> ehcache-core< / artifactId>
< version> 2.2.0< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-core< / artifactId>
< version> 3.3.2.GA< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-commons-annotations< / artifactId>
< version> 3.3.0.ga< / version>
<排除项>
<排除>
< groupId> net.sf.ehcache< / groupId>
< artifactId> ehcache< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-annotations< / artifactId>
< version> 3.2.1.ga< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> ejb3-persistence< / artifactId>
< version> 3.3.2.Beta1< / version>
< /依赖关系>
I'm working with a spring project using hibernate and look to implement second-level cache using ehcache. I see a number of approaches to this:
Personally I don't think spring-modules-cache
is thorough enough, hence I would probably prefer consider the more actively developed ehcache-spring-annotations
. Hibernate cache
though seems to be most complete implementation (e.g., both read and write cache etc).
What would motivate which toolset to use? Please share your caching experience ...
Our project uses option 3. We apply annotation org.hibernate.annotations.Cache
to entities that we cache in an Ehcache, configure Ehcache using ehcache.xml
, and enable and configure the Hibernate second-level cache in hibernate.cfg.xml
:
<!-- 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>
For most entities, we use cache concurrency strategy CacheConcurrencyStrategy.TRANSACTIONAL
:
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
Our Maven project uses Hibernate 3.3.2GA and 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:hibernate + ehcache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!