1.文件转载于:http://hi.baidu.com/xusuofei/blog/item/609468d0bc374e8ba0ec9c88.html
2.配置二级缓存的主要步骤:
(1)准备
把ehcache-1.2.3.jar加入到classpath中
在hibernate.cfg.xml中加入EHCache缓存插件的提供类
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>
<?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>
<?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> 设定具体的命名缓存的数据过期策略
每个命名缓存代表一个缓存区域,每个缓存区域有各自的数据过期策略。命名缓存机制使得用户能够在每个类以及类的每个集合的粒度上设置数据过期策略。
cache元素的属性
name 设置缓存的名字,它的取值为类的全限定名或类的集合的名字
maxInMemory 设置基于内存的缓存中可存放的对象最大数目
eternal 设置对象是否为永久的,true表示永不过期,此时将忽略timeToIdleSeconds和timeToLiveSeconds属性;
默认值是false
timeToIdleSeconds 设置对象空闲最长时间,超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。
如果此值为0,表示对象可以无限期地处于空闲状态。
timeToLiveSeconds 设置对象生存最长时间,超过这个时间,对象过期。
如果此值为0,表示对象可以无限期地存在于缓存中。
overflowToDisk 设置基于内在的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中
元素或属性 描述
<diskStore> 设置缓存数据文件的存放目录
<defaultCache> 设置缓存的默认数据过期策略
<cache> 设定具体的命名缓存的数据过期策略
每个命名缓存代表一个缓存区域,每个缓存区域有各自的数据过期策略。命名缓存机制使得用户能够在每个类以及类的每个集合的粒度上设置数据过期策略。
cache元素的属性
name 设置缓存的名字,它的取值为类的全限定名或类的集合的名字
maxInMemory 设置基于内存的缓存中可存放的对象最大数目
eternal 设置对象是否为永久的,true表示永不过期,此时将忽略timeToIdleSeconds和timeToLiveSeconds属性;
默认值是false
timeToIdleSeconds 设置对象空闲最长时间,超过这个时间,对象过期。当对象过期时,EHCache会把它从缓存中清除。
如果此值为0,表示对象可以无限期地处于空闲状态。
timeToLiveSeconds 设置对象生存最长时间,超过这个时间,对象过期。
如果此值为0,表示对象可以无限期地存在于缓存中。
overflowToDisk 设置基于内在的缓存中的对象数目达到上限后,是否把溢出的对象写到基于硬盘的缓存中