第一步 新建ehcache.xml 放到 src 目录下,以下文件具体参数意义,我这不多说了,你从百度查一下“ehcache 配置” 会有很详细的介绍, |
02 |
在这呢,我只说一下,基本配置,并如何知道已成功应用 二级缓存 |
03 |
< ehcache Check = "false" > |
04 |
< diskStore path = "java.io.tmpdir" /> |
05 |
< cacheManagerEventListenerFactory class = "" properties = "" /> |
07 |
maxElementsInMemory = "1000" |
10 |
timeToIdleSeconds = "120" |
11 |
timeToLiveSeconds = "180" |
12 |
diskPersistent = "false" |
13 |
diskExpiryThreadIntervalSeconds = "60" /> |
15 |
第二步 hibernate 参数配置。 以下为 spring applicationContext 内配置方式 |
1 |
< prop key = "net.sf.ehcache.configurationResourceName" >ehcache.xml</ prop > |
2 |
< prop key = "hibernate.cache.use_second_level_cache" >true</ prop > |
3 |
< prop key = "hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</ prop > |
4 |
< prop key = "hibernate.generate_statistics" >true</ prop > |
第三步 在被应用缓存的XXX.hbm.xml 文件中加入下
1 |
< cache usage = "read-write" /> 详细参数 自己查询 |
第四步 打开日志
log4j.logger.net.sf.ehcache=debug
注意:
1. 如果使用 getHibernateTemplate 查询要设置以下属性
1 |
getHibernateTemplate().setCacheQueries( true ); |
2. 加入以下代码查看 是否 配置成功
01 |
getHibernateTemplate().execute( new HibernateCallback() |
04 |
public Object doInHibernate(Session arg0) throws HibernateException, |
07 |
Statistics message=arg0.getSessionFactory().getStatistics(); |
08 |
System.out.println( "二级缓存命中数:" +message.getSecondLevelCacheHitCount()); |
如第二次查询 二级缓存命中数大于0 配置成功