小编典典

春季:休眠+ ehcache

java

我正在使用hibernate处理spring项目,并希望使用ehcache实现二级缓存。我看到了许多解决方法:

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

  2. ehcache-spring-annotations一个旨在成为继任者的工具集spring-modules-cache

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

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

就我个人而言,我认为spring-modules-cache还不够彻底,因此我可能更愿意考虑发展得更为积极ehcache-spring- annotationsHibernate cache尽管这似乎是最完整的实现(例如,读取和写入缓存等)。

是什么促使使用哪个工具集?请分享您的缓存经验…


阅读 205

收藏
2020-11-16

共1个答案

小编典典

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

对于大多数实体,我们使用缓存并发策略CacheConcurrencyStrategy.TRANSACTIONAL

@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)

我们的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>
2020-11-16