如何在Hibernate中启用二级缓存 [英] How to enable second level cache in Hibernate

查看:79
本文介绍了如何在Hibernate中启用二级缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中需要一些pojo对象,所以我想知道如何启用二级缓存。到目前为止,默认情况下第一级缓存已启用,我也想知道第二级缓存有哪些优缺点。

这是你需要做的:


  1. 设置下面的Hibernate属性:

     < property name =hibernate.cache.use_second_level_cache> true< / property> 
    < property name =hibernate.cache.provider_class> org.hibernate.cache.EhCacheProvider< / property>


  2. 添加 ehcache.xml文件,包含缓存配置条目:

      maxElementsInMemory =50
    eternal =true
    overflowToDisk =false
    timeToIdleSeconds =600
    timeToLiveSeconds =600
    diskPersistent =false
    memoryStoreEvictionPolicy =LRU
    />


  3. 为每个实体定义缓存类型:

      @Entity 
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public class MyEntity {
    ...
    }



I need some pojo objects across my application so I want to know how to enable Second Level Cache. Until now by default First Level cache is enabled, I would also like to know what advantages and disadvantages of Second Level cache there are.

This is what you need to do:

  1. Set the following Hibernate properties:

    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
    

  2. Add an ehcache.xml file in your classpath, containing the cache configuration entries:

    <cache name="com.mycompany.MyEntity"
       maxElementsInMemory="50"
       eternal="true"
       overflowToDisk="false"
       timeToIdleSeconds="600"
       timeToLiveSeconds="600"
       diskPersistent="false"
       memoryStoreEvictionPolicy="LRU"       
    />
    

  3. Define the Caching type for each entity:

    @Entity
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
    public class MyEntity {
            ...
    }
    

这篇关于如何在Hibernate中启用二级缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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