java 春天:休眠 + ehcache
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5270998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: hibernate + ehcache
提问by Johan Sj?berg
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:
我正在使用 hibernate 处理一个 spring 项目,并希望使用 ehcache 实现二级缓存。我看到了许多方法:
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)
spring-modules-cache
其中介绍了@Cacheable
注释ehcache-spring-annotations
旨在成为spring-modules-cache
.Hibernate cache
很好地集成到休眠本身以使用例如@Cache
注释执行缓存。Programmatic cache
使用代理。基于注释的配置很快变得有限或复杂(例如,多个级别的注释嵌套)
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).
我个人认为spring-modules-cache
不够彻底,因此我可能更愿意考虑更积极开发的ehcache-spring-annotations
. Hibernate cache
虽然似乎是最完整的实现(例如,读取和写入缓存等)。
What would motivate which toolset to use? Please share your caching experience ...
什么会促使使用哪个工具集?请分享您的缓存经验...
采纳答案by Derek Mahar
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 cachein hibernate.cfg.xml
:
我们的项目用途选项3.我们应用注释org.hibernate.annotations.Cache
的实体,我们缓存在了Ehcache,配置了Ehcache使用ehcache.xml
,并启用和配置Hibernate的二级缓存在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
:
对于大多数实体,我们使用缓存并发策略CacheConcurrencyStrategy.TRANSACTIONAL
:
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
Our Maven project uses Hibernate 3.3.2GA and Ehcache 2.2.0:
我们的 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>