博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate JPA 中配置Ehcache二级缓存
阅读量:4221 次
发布时间:2019-05-26

本文共 3308 字,大约阅读时间需要 11 分钟。

在Hibernate3 JPA里配置了一下非分布式环境的二级缓存,效果不错。具体过程如下:

 

1, 需要引入的jar包

       下载的包里已经包含了简单的例子和javadoc

 

      ehcache-core-2.4.6.jar (必需)

      ehcache-terracotta-2.4.6.jar (必需)

      slf4j-api-1.6.1.jar

      slf4j-jdk14-1.6.1.jar

 

 

2, 在JPA的persistence.xml中加入以下配置

 

      <property name="hibernate.cache.provider_class" 

                value="org.hibernate.cache.SingletonEhCacheProvider" />

     <property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />
     <property name="hibernate.cache.use_second_level_cache" value="true" />
     <property name="hibernate.cache.use_query_cache" value="true" />

 

 

3, 对ehcache进行简单的设置(ehcache.xml

      <?xml version="1.0" encoding="UTF-8"?>

      <ehcache>

<!-- 
maxElementsInMemory:缓存中最大允许创建的对象数
eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期
timeToIdleSeconds:缓存数据钝化时间(设置对象在它过期之前的空闲时间)
timeToLiveSeconds:缓存数据的生存时间(设置对象在它过期之前的生存时间)
overflowToDisk:内存不足时,是否启用磁盘缓存
clearOnFlush:内存数量最大时是否清除

  -->

      <defaultCache maxElementsInMemory="1000" eternal="false"
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
      </defaultCache>

       <!-- 单独对某个entity的缓存策略设置-->

      <cache name="com.payment.entity.PromotionEntity" maxElementsInMemory="100"

           eternal="false"

           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
      </cache>
     </ehcache>

 

 

4, JPA的Entity类中声明缓存的隔离机制

 

       import org.hibernate.annotations.Cache;

       import org.hibernate.annotations.CacheConcurrencyStrategy;

 

      @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

      @Entity
      @Table(name = "catagory")
      public class CatagoryEntity extends BaseEntity { ... }

 

 

5, 如何使用二级缓存中的对象

       在Hibernate中可以通过org.hibernate.Query.setCacheable(true);

       在JPA中,由于EntityManager中得到的javax.persistence.Query没有这个方法了。我们可以通过

       javax.persistence.Query.setHint(”org.hibernate.cacheable”, true);来实现读取二级缓存。

 

 

 

6, 在log4j输出日志中可以看到缓存机制作用

 

      18:05:30,682 DEBUG SessionImpl:265 - opened session at timestamp: 5410486397673472

      18:05:30,682 DEBUG StandardQueryCache:125 - checking cached query results in region:     

          org.hibernate.cache.StandardQueryCache

      18:05:30,682 DEBUG EhCache:74 - key: sql: select promotione0_.id as id2_, 

          promotione0_.catagory_id as catagory6_2_, promotione0_.description as descript2_2_, 

          promotione0_.enabled as enabled2_, promotione0_.name as name2_, promotione0_.picture as

          picture2_, promotione0_.product_id as product7_2_ from promotion promotione0_; parameters:

          ; named parameters: {}

      18:05:30,682 DEBUG StandardQueryCache:183 - Checking query spaces for up-to-dateness:

          [promotion]

      18:05:30,682 DEBUG EhCache:74 - key: promotion
      18:05:30,682 DEBUG EhCache:83 - Element for promotion is null
      18:05:30,682 DEBUG StandardQueryCache:140 - returning cached query results
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#1
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#2
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#3
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#4
      18:05:30,714 DEBUG StatefulPersistenceContext:893 - initializing non-lazy collections
      18:05:30,714 DEBUG EhCache:74 - key: com.payment.entity.PromotionEntity#5

转载地址:http://toemi.baihongyu.com/

你可能感兴趣的文章
Intellij Idea 工具在java文件中怎么避免 import .*包,以及import包顺序的问题
查看>>
IDEA Properties中文unicode转码问题
查看>>
Oracle中Blob转换成Clob
查看>>
Linux如何查看so中函数名
查看>>
自动管理代码的android.mk
查看>>
cocos2dx 2.2.6编译记录(1)
查看>>
makefile学习网站
查看>>
C 编写lua模块(1)
查看>>
Lua教程:Lua调用C/C++函数(4)
查看>>
win下创建win32控制台工程,执行lua脚本
查看>>
cocos2dx android启动错误
查看>>
eclipse: android rename package name
查看>>
cocos2dx c++调用java思想
查看>>
cocos2dx lua Node节点 私有数据存取
查看>>
lua math.ceil math.ceil
查看>>
cocos2dx CCNode计算node的大小
查看>>
cocos2dx 布局记录(1)
查看>>
lua 多行注释和取消多行注释
查看>>
缩放系数计算
查看>>
cocos2dx --- 按钮点击居中放大
查看>>