使用EhCache配置二级缓存

1、配置准备:
1) 把ehcache-1.2.3.jar加入到当前应用的classpath中。
2) 在hibernate.cfg.xml文件中加入EhCache缓存插件的提供类。
<!--配置缓存插件 -->
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>

3)挎贝ehcache.xml文件到类路径(项目工程的src目录下),这个文件在Hibernate安装目录的etc下。
2、配置步骤:
Hibernate允许在类和集合的粒度上设置第二级缓存。在映射文件中,<class>和<set>元素都有一个<cache>子元素,这个子元素用来配置二级缓存。
示例:以category(产品类别)和product(产品)的映射为例:
1) 修改要配置缓存的那个持久化类的对象关系映射文件:
Category.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.qiujy.domain.cachedemo.Category" table="categories">
<!—
配置缓存,必须紧跟在class元素后面
对缓存中的Category对象采用读写型的并发访问策略
-->
<cache usage="read-write"/>

<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="native" />
</id>
<!-- 配置版本号,必须紧跟在id元素后面 -->
<version name="version" column="version" type="java.lang.Long" />

<property name="name" type="java.lang.String">
<column name="name" length="32" not-null="true"/>
</property>

<property name="description" type="java.lang.String">
<column name="description" length="255"/>
</property>

<set name="products" table="products" cascade="all" inverse="true">
<!-- Hibernate只会缓存对象的简单属性的值,
要缓存集合属性,必须在集合元素中也加入<cache>子元素
而Hibernate仅仅是把与当前持久对象关联的对象的OID存放到缓存中。
如果希望把整个关联的对象的所有数据都存入缓存,
则要在相应关联的对象的映射文件中配置<cache>元素
-->
<cache usage="read-write"/>

<key column="categoryId" not-null="true"/>
<one-to-many class="org.qiujy.domain.cachedemo.Product"/>
</set>

</class>
</hibernate-mapping>

Product.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.qiujy.domain.cachedemo.Product" table="products">

<cache usage="read-write"/>

<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="native" />
</id>
<!-- 配置版本号,必须紧跟在id元素后面 -->
<version name="version" column="version" type="java.lang.Long" />

<property name="name" type="java.lang.String">
<column name="name" length="32" not-null="true"/>
</property>

<property name="description" type="java.lang.String">
<column name="description" length="255"/>
</property>

<property name="unitCost" type="java.lang.Double">
<column name="unitCost" />
</property>

<property name="pubTime" type="java.util.Date">
<column name="pubTime" not-null="true" />
</property>

<many-to-one name="category"
column="categoryId"
class="org.qiujy.domain.cachedemo.Category"
cascade="save-update"
not-null="true">
</many-to-one>

</class>
</hibernate-mapping>

2) 编辑ehcache.xml文件:
<ehcache>
<diskStore path="c:\\ehcache\"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>

<!-- 设置Category类的缓存的数据过期策略 -->
<cache name="org.qiujy.domain.cachedemo.Category"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>

<!-- 设置Category类的products集合的缓存的数据过期策略 -->
<cache name="org.qiujy.domain.cachedemo.Category.products"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>

<cache name="org.qiujy.domain.cachedemo.Product"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>

</ehcache>

配置的元素说明:
元素或属性

描述
<diskStore> 设置缓存数据文件的存放目录
<defaultCache> 设置缓存的默认数据过期策略
<cache> 设定具体的命名缓存的数据过期策略
每个命名缓存代表一个缓存区域,每个缓存区域有各自的数据过期策略。命名缓存机制使得用户能够在每个类以及类的每。。。

[url]http://hain.iteye.com/blog/152808[/url]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值