有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javaspring:hibernate+ehcache

我正在使用hibernate处理一个spring项目,并希望使用ehcache实现二级缓存。我认为有很多方法可以做到这一点:

  1. ^{}它引入了@Cacheable注释

  2. ^{}一个旨在成为^{的继承者的工具集

  3. ^{}很好地集成到hibernate本身中,以使用@Cache注释执行缓存

  4. ^{}使用代理。基于注释的配置很快就会变得有限或复杂(例如,注释嵌套的几个级别)

个人认为,{{CD1>}是足够彻底的,因此我可能更倾向于考虑更积极发展的{{CD3}}。^。{}不过似乎是最完整的实现(例如,读缓存和写缓存等)

什么会促使使用哪种工具集?请分享您的缓存体验


共 (2) 个答案

  1. # 1 楼答案

    我们的项目使用选项3。我们将注释org.hibernate.annotations.Cache应用于缓存在Ehcache中的实体,使用ehcache.xml配置Ehcache,并在hibernate.cfg.xml中启用和配置Hibernate second-level cache

        <!  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>
    

    对于大多数实体,我们使用缓存并发策略^{}

    @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>
    
  2. # 2 楼答案

    Spring3.1有一个新的内置缓存抽象Read here