• Hibernate4.1.4配置二级缓存EHCache步骤


    1、当然首先引入EHCache相关的jar包

    这些包不需要另外下载,在Hibernate官方网站下载Hibernate4.1.7的压缩包(如:hibernate-release-4.1.7.Final.zip)解压,引入hibernate-release-4.1.7.Finalhibernate-release-4.1.7.Finalliboptionalehcache目录下的ehcache-core-2.4.3.jar、hibernate-ehcache-4.1.4.Final.jar、slf4j-api-1.6.1.jar三个jar包即可(就是添加到referenced libraries下)。

    2、在项目classpath的根目录下面写好EHCache的配置文件ehcache.xml

    此配置文件可以直接到HIBERNATE_HOMEprojectetcehcache.xml拷贝到src目录下即可。(里面有3个例子,只需要一个用默认缓存配置就可以)

    1. <?xml version="1.0" encoding="UTF-8"?>   
    2. <ehcache>   
    3.  <diskStore path="java.io.tmpdir"/>   
    4.   <defaultCache   
    5.    maxElementsInMemory="10000" <!-- 缓存最大数目 -->   
    6.    eternal="false" <!-- 缓存是否持久 -->   
    7.    overflowToDisk="true" <!-- 是否保存到磁盘,当系统当机时-->   
    8.    timeToIdleSeconds="300" <!-- 当缓存闲置n秒后销毁 -->   
    9.    timeToLiveSeconds="180" <!-- 当缓存存活n秒后销毁-->   
    10.    diskPersistent="false"   
    11.    diskExpiryThreadIntervalSeconds= "120"/>   
    12. </ehcache>   

    3、在Hibernate配置文件里面启用EHCache

    说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用 findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话, 就需要在配置文件中设置hibernate.cache.use_query_cache true 才行

    1. <!-- 开启二级缓存 -->  
    2. <property name="cache.use_second_level_cache">true</property>  
    3.  <!-- 开启查询缓存 -->    
    4. <property name="hibernate.cache.use_query_cache">true</property>                   
    5. <!-- 配置RegionFactory为Ehcache的RegionFactory -->  
    6. <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>  


    4、配置哪些实体类的对象需要二级缓存

    在Hibernate配置文件hibernate.cfg.xml中统一配置(推荐)

      注意,这个<class-cache>标签必须放在<mapping>标签的后面!!

    [html] view plaincopy
     
    1. <!--   
    2.         配置哪些实体类的对象需要二级缓存  
    3.         usage属性为缓存策略  
    4. -->  
    5. <class-cache usage="read-only" class="com.ru.domain.Student"/>  


     

    说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用 findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话, 就需要在配置文件中设置 hibernate.cache.use_query_cache true 才行 

      1. <!-- 开启查询缓存 -->    
      2. <property name="hibernate.cache.use_query_cache">true</property>    
  • 相关阅读:
    移除TDE
    SQL Server 聚集索引和非聚集索引的区别
    Serivce Broker 简单实用
    SQL Server 2012 ColumnStore索引测试
    UISegmentedControl的所有操作总结
    iPhone开发之深入浅出 — ARC之前世今生(三)
    什么是 ARC?ios5,xcode 4.2
    Present ViewController详解
    UITextField的总结
    iPhone开发资料之内存管理 ,循环引用导致的内存问题
  • 原文地址:https://www.cnblogs.com/sandea/p/3818601.html
Copyright © 2020-2023  润新知