码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate Cache :使用Ehcache作为Hibernate的二级缓存的配置说明

时间:2014-09-01 00:12:32      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   使用   java   strong   ar   

 

Ehcache介绍

    Ehcache是一个快速的、轻量级Java应用缓存。Hibernate中就支持了Ehcache。

http://ehcache.org/documentation/integrations/hibernate

 

Hibernate与Ehcache集成

要完成Hibernate与Ehcache的集成,只需要按照下面几步操作即可完成。

1、下载Ehcache-core 包

可以在浏览器上输入网址:http://sourceforge.net/projects/ehcache/files/ehcache-core/ 选择相应版本下载。

也可以使用Maven依赖:

<dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache-core</artifactId>
      <version>2.6.9</version>
</dependency>

 

2、添加Hibernate-Ehcache包 

因为要与Hibernate匹配使用,所以要根据Hibernate的版本来下载Ehcache插件包。

<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>${hibernate-version}</version>
</dependency>

 

3、在hibernate.cfg.xml中配置Ehcache作为二级缓存

Ehcache是作为Hibernate中的二级缓存使用的。

对于Hibernate3.3以上的版本:

bubuko.com,布布扣

也可以使用:

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

对于Hibernate 4.X版本:

因为Hibernate4.x版本将Hibernate-Ehcache.jar集成到Hibernate-core.jar中了。所以上面的配置有所改变:

 bubuko.com,布布扣

也可以使用:

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

 

4、开启二级缓存或者查询缓存

<!--二级缓存-->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--查询缓存-->
<property name="hibernate.cache.use_query_cache">true</property>

5、在ehcache.xml配置中配置多个缓存

 

如果我们项目中,没有配置ehcache.xml,就会使用默认的配置文件:ehcache-failsafe.xml,这个文件可以在Ehcache-core.jar中找到。

一个mapper中,可以配置各种缓存策略。既可以为实体配置缓存,也可以为实体中的集合配置缓存,当然还可以为查询配置缓存。

 bubuko.com,布布扣

为了展示这三种缓存配置方式,把他们都放到一个文件中。上面只是展示了有这三种Cache配置方式。接下来,就对这三种缓存配置方式分别看看ehcache是如何配置的。

 Ehcache.xml中有两种常用的元素:<diskStore>、 <cache>

 <diskStore path="java.io.tmpdir"/>

用于配置使用磁盘存储时磁盘的位置。

 

<Cache>配置说明

在了解ehcache配置之前,先来了解一下ehcache的各项属性

 bubuko.com,布布扣

 

必要的属性:

name :是cache的名字,在ehcache.xml文件中是唯一的。命名约束根据三种级别的缓存是不同的。

maxElementsInMemory: 在内存中对象的最大数目。

maxElementsOnDisk: 在磁盘中存储的对象的最大数目。

eternal:永久的。如果设置永久的,就会忽略掉 对象缓存时间的限制。

overflowToDisk: 设置在内存中对象的数目超出最大值时,是否存储到磁盘上。

 

可选的属性:

timeToIdleSeconds: 设置在对象过期前的空闲时间。默认值是0

timeToLiveSeconds: 设置在对象过期前的存活时间。默认值是0

 

diskPersistent: 设置当JVM重启时,是否在磁盘上持久化对象。

diskSpoolBufferSizeMB: 设置磁盘上缓冲区大小。

memoryStoreEvictionPolicy: 设置内存达到maxElementsInMemory时的清除策略。默认策略是LRU(最近最少使用),也有其他的策略,如:FIFO(先进先出策略),LFU(最少使用策略)。

 

 

接下来,就分别说说这三种级别的缓存。

三种级别的<cache>的name属性设置

1)实体级别的缓存

 

实体级别的缓存,ehcache.xml中<cache>元素的name属性的命名方式是:类的全名。例如项目中有个类:com.fjn.other.hibernateCache.entity.Event

那么应配置为:

<cache name=” com.fjn.other.hibernateCache.entity.Event”
其他属性
/>

  

2)集合级别缓存

这里说的集合级别的缓存,其实是实体中某个属性是一个集合。例如:

public calss Customer{
    private String name;
    private List<Address> adds;
}

这样一个类,如果是对Customer的属性adds使用缓存,也就是使用了集合级别的缓存,它的mapper文件中应该会是像这样的配置: 

<list name="adds">
    <cache usage="read-only"/>
</list>

这种情况下,我们就可以在ehcache.xml中做如下配置:

<cache name=” com.fjn.other.hibernateCache.entity.Customer.adds”
 其他属性
/>

 

3)查询级别的缓存

 

这个其实,就是前一节中学习到的Query Cache。

 

使用Query Cache时,可以设置name,也可以不设置name。如果不设置name,那么默认的name是:org.hibernate.cache.StandardQueryCache,或者是org.hibernate.cache.UpdateTimestampsCache。

这个在自己测试时,使用Debug就可以看得到。

 bubuko.com,布布扣

 bubuko.com,布布扣

 

上面是不指定名字是默认的值。如果自己指定了name呢?

 

如果要自己指定查询缓存的name属性,一般使用query.YourCacheName 。也就是一般会使用query.开头。

在程序中如此使用:

Query query=session.createQuery(hql);
query.setCacheable(true);
query.setCacheRegion(“query.YourCacheName”);

  

6、在hibernate.cfg.xml中配置ehcache.xml的配置文件的位置

 

在hibernate.cfg.xml中配置ehcache.xml的位置,例如:

<property name="cache.provider_configuration_file_resource_path">config/hibernate/ehcache/ehcache.xml</property>

 


  

Hibernate Cache :使用Ehcache作为Hibernate的二级缓存的配置说明

标签:style   blog   http   color   io   使用   java   strong   ar   

原文地址:http://www.cnblogs.com/f1194361820/p/3948338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!