元素或属性
|
描述
|
<diskStore>
|
设置缓存数据文件的存放目录
|
<defaultCache>
|
设置缓存的默认数据过期策略
|
<cache>
|
设定具体的命名缓存的数据过期策略
每个命名缓存代表一个缓存区域,每个缓存区域有各自的数据过期策略。命名缓存机制使得用户能够在每个类以及类的每个集合的粒度上设置数据过期策略。
|
cache元素的属性
|
|
name
|
设置缓存的名字
,
它的取值为类的全限定名或类的集合的名字
|
maxInMemory
|
设置基于内存的缓存中可存放的对象最大数目
|
eternal
|
设置对象是否为永久的
,true
表示永不过期
,
此时将忽略
timeToIdleSeconds
和
timeToLiveSeconds
属性
;
默认值是
false
|
timeToIdleSeconds
|
设置对象空闲最长时间
,
超过这个时间
,
对象过期。当对象过期时
,EHCache
会把它从缓存中清除。
如果此值为
0,
表示对象可以无限期地处于空闲状态。
|
timeToLiveSeconds
|
设置对象生存最长时间
,
超过这个时间
,
对象过期。
如果此值为
0,
表示对象可以无限期地存在于缓存中。
|
overflowToDisk
|
设置基于内在的缓存中的对象数目达到上限后
,
是否把溢出的对象写到基于硬盘的缓存中
|
package org.qiujy.test.cache;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.qiujy.common.HibernateSessionFactory;
import org.qiujy.domain.cachedemo.Product;
public class TestCache {
public static void main(String[] args) {
//test cache.........
Session session2 = HibernateSessionFactory.getSession();
Transaction tx2 =null;
try{
tx2 = session2.beginTransaction();
List list = session2.createQuery("from Product").list();
for(int i = 0 ; i < list.size(); i++){
Product prod = (Product)list.get(i);
System.out.println(prod.getName());
}
tx2.commit();
}catch(HibernateException e){
if(tx2 != null){
tx2.rollback();
}
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
//-------------------
Session session3 = HibernateSessionFactory.getSession();
Transaction tx3 =null;
try{
tx3 = session3.beginTransaction();
Product prod = (Product)session3.get(Product.class, new Long(1));
System.out.println("从cache中得到,不执行SQL---" + prod.getName());
tx3.commit();
}catch(HibernateException e){
if(tx3 != null){
tx3.rollback();
}
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
}
|
<!--
设置默认的查询缓存的数据过期策略
-->
<
cache
name
=
"org.hibernate.cache.StandardQueryCache"
maxElementsInMemory
=
"50"
eternal
=
"false"
timeToIdleSeconds
=
"3600"
timeToLiveSeconds
=
"7200"
overflowToDisk
=
"true"
/>
<!--
设置时间戳缓存的数据过期策略
-->
<
cache
name
=
"org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory
=
"5000"
eternal
=
"true"
overflowToDisk
=
"true"
/>
<!--
设置自定义命名查询缓存
customerQueries
的数据过期策略
-->
<
cache
name
=
"myCacheRegion"
maxElementsInMemory
=
"1000"
eternal
=
"false"
timeToIdleSeconds
=
"300"
timeToLiveSeconds
=
"600"
overflowToDisk
=
"true"
/>
|
<!--
启用查询缓存
-->
<
property
name
=
"cache.use_query_cache"
>
true
</
property
>
|
package org.qiujy.test.cache;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.qiujy.common.HibernateSessionFactory;
import org.qiujy.domain.cachedemo.Product;
public class TessQueryCache {
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
Transaction tx =null;
try{
tx = session.beginTransaction();
Query query = session.createQuery("from Product");
//
激活查询缓存
query.setCacheable(true);
//
使用自定义的查询缓存区域
,
若不设置
,
则使用标准查询缓存区域
query.setCacheRegion("myCacheRegion");
List list = query.list();
for(int i = 0 ; i < list.size(); i++){
Product prod = (Product)list.get(i);
System.out.println(prod.getName());
}
tx.commit();
}catch(HibernateException e){
if(tx != null){
tx.rollback();
}
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
}
|