简体   繁体   English

Spring:hibernate + ehcache

[英]Spring: hibernate + ehcache

I'm working with a spring project using hibernate and look to implement second-level cache using ehcache. 我正在使用一个使用hibernate的spring项目,并期望使用ehcache实现二级缓存。 I see a number of approaches to this: 我看到了很多方法:

  1. spring-modules-cache which introduces the @Cacheable annotation spring-modules-cache引入了@Cacheable注释

  2. ehcache-spring-annotations a toolset which aims to be the successor of spring-modules-cache . ehcache-spring-annotations一个工具集,旨在成为spring-modules-cache的继承者。

  3. Hibernate cache is nicely integrated into hibernate itself to perform caching using eg, the @Cache annotation. Hibernate cache很好地集成到hibernate本身,以使用例如@Cache注释执行缓存。

  4. Programmatic cache using proxies. 使用代理的Programmatic cache Annotation based config quickly becomes either limited or complex (eg, several levels of annotation nesting) 基于注释的配置很快变得有限或复杂(例如,几个级别的注释嵌套)

Personally I don't think spring-modules-cache is thorough enough, hence I would probably prefer consider the more actively developed ehcache-spring-annotations . 我个人认为spring-modules-cache不够彻底,因此我可能更愿意考虑更积极开发的ehcache-spring-annotations Hibernate cache though seems to be most complete implementation (eg, both read and write cache etc). Hibernate cache虽然似乎是最完整的实现(例如,读取和写入缓存等)。

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 : 我们的项目使用选项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>

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>

Spring 3.1 has a new built-in cache abstraction. Spring 3.1有一个新的内置缓存抽象。 Read here . 在这里阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM