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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:20:56  来源:igfitidea点击:

Spring: hibernate + ehcache

javahibernatespringcachingehcache

提问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 实现二级缓存。我看到了许多方法:

  1. spring-modules-cachewhich introduces the @Cacheableannotation

  2. ehcache-spring-annotationsa toolset which aims to be the successor of spring-modules-cache.

  3. Hibernate cacheis nicely integrated into hibernate itself to perform caching using e.g., the @Cacheannotation.

  4. Programmatic cacheusing proxies. Annotation based config quickly becomes either limited or complex (e.g., several levels of annotation nesting)

  1. spring-modules-cache其中介绍了@Cacheable注释

  2. ehcache-spring-annotations旨在成为spring-modules-cache.

  3. Hibernate cache很好地集成到休眠本身以使用例如@Cache注释执行缓存。

  4. Programmatic cache使用代理。基于注释的配置很快变得有限或复杂(例如,多个级别的注释嵌套)

Personally I don't think spring-modules-cacheis thorough enough, hence I would probably prefer consider the more actively developed ehcache-spring-annotations. Hibernate cachethough 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.Cacheto 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>

回答by Bozho

Spring 3.1 has a new built-in cache abstraction. Read here.

Spring 3.1 有一个新的内置缓存抽象。在这里阅读