Spring:hibernate + ehcache [英] Spring: hibernate + ehcache

查看:66
本文介绍了Spring:hibernate + ehcache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate开发一个spring项目,并使用ehcache来实现二级缓存。我看到了很多方法:


  1. spring-modules-cache ,它引入了 @Cacheable 注释

  2. .google.com / p / ehcache-spring-annotations /rel =noreferrer> ehcache-spring-annotations 一个旨在成为继任者的工具集 spring-modules-cache
  3. Hibernate缓存 很好地集成到hibernate本身中来执行缓存,例如使用 @Cache 注释。

  4. 编程缓存 使用代理。基于注解的配置很快就会变得有限或者复杂(例如,多层次的注释嵌套)。


    我个人不会认为 spring-modules-cache 已经足够透彻,因此我可能更愿意考虑更积极地开发 ehcache-spring-annotations 。尽管Hibernate缓存似乎是最完整的实现(例如,读写缓存等)。

    什么会激发哪个工具集使用?请分享您的缓存体验......我们的项目使用选项3.我们应用注释 org。

    hibernate.annotations.Cache 到我们在 Ehcache 中缓存的实体,使用<$配置Ehcache c $ c> ehcache.xml ,并启用并配置 Hibernate二级缓存 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:

    1. spring-modules-cache which introduces the @Cacheable annotation

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

    3. Hibernate cache is nicely integrated into hibernate itself to perform caching using e.g., the @Cache annotation.

    4. Programmatic cache using proxies. Annotation based config quickly becomes either limited or complex (e.g., 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. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆