Spring:hibernate + ehcache
我正在使用一个使用hibernate的spring项目,并期望使用ehcache实现二级缓存。 我看到了很多方法:
-
spring-modules-cache
引入了@Cacheable
注释 -
ehcache-spring-annotations
一个工具集,旨在成为spring-modules-cache
的inheritance者。 -
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二级缓存 :
net.sf.ehcache.hibernate.EhCacheProvider net.sf.ehcache.hibernate.EhCacheRegionFactory true true true true
对于大多数实体,我们使用缓存并发策略CacheConcurrencyStrategy.TRANSACTIONAL
:
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
我们的Maven项目使用Hibernate 3.3.2GA和Ehcache 2.2.0:
net.sf.ehcache ehcache-core 2.2.0 org.hibernate hibernate-core 3.3.2.GA org.hibernate hibernate-commons-annotations 3.3.0.ga net.sf.ehcache ehcache org.hibernate hibernate-annotations 3.2.1.ga org.hibernate ejb3-persistence 3.3.2.Beta1
Spring 3.1有一个新的内置缓存抽象。 在这里阅读