Hibernate JPA 中配置Ehcache二级缓存

时间:2023-03-08 16:50:15

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

1, 需要引入的jar包

http://ehcache.org/downloads/catalog 下载的包里已经包含了简单的例子和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