前言
废话不多说,来这看的,不管你是会ehcache或者从来没接触过的,保证看过之后,都能构建一个自己的ehcache项目。因为我在写这篇博客的时候,是一边在配置一边在记录过程中遇到的问题,等于是在手把手教学。下面直入正题。
一、下载相关jar包
网上下载ehcache-core-1.3.0.jar这个jar包。
然后项目引用日志包:slf4j-api-1.7.5.jar、slf4j-log4j12-1.7.5.jar、log4j-1.2.12.jar
建议:不要使用与hibernate不匹配的ehcache版本,如ehche-core-2.7.0,否则可能客户端代码出现异常
二、配置相关配置文件
新建ehcache.xml,放在src目录下,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<!--给工具箱加缓存,工具箱不经常变化-->
<cache name="CAHCE_FOR_TOOLSBOX"
maxElementsInMemory="20000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
至于里面的这些属性是什么意思,额,一时半会也解释不清楚,这个百度上也挺多的,我就不给你们普及这里的参数意思了。
直接编写一段测试代码,先来测试下:
/*
* 文 件 名 : com.TestCache.java
* 创建日期 : 2013-10-20 13:31:37
* 创 建 者 : qsyang
*/
package com;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* @author qsyang
* @version 1.0
*/
public class TestCache {
CacheManager cacheManager = new CacheManager();
public void runCache() {
Cache cache = cacheManager.getCache("CAHCE_FOR_TOOLSBOX");
cache.put(new Element("testKey", "testValue"));
for (Object key : cache.getKeys()) {
System.out.println("Key:" + key + ",value:" + cache.get(key).getObjectValue());
}
cacheManager.shutdown();
}
public static void main(String[] args) throws Exception {
TestCache cl = new TestCache();
cl.runCache();
}
}
打印结果:Key:testKey,value:testValue
缓存配置成功!
三、配置hibernate配置文件,并添加二级缓存配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">12344</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>
<mapping resource="com/qisentech/hibernate/po/UserPO.hbm.xml"/>
</session-factory>
</hibernate-configuration>
有的人如果出现
添加diskSpoolBufferSizeMB参数报错的问题
那么说明是ehcache版本问题,建议还是使用我指定的版本,有兴趣可以研究下其他版本。
然后测试一下二级缓存!
Session s = null;
try {
s = HibernateUtil.getSession();
Query query = s.createQuery("from User");
//设置使用缓存
query.setCacheable(true);
List<User> list1 = query.list();
System.out.println(list1.size());
List<User> list2 = query.list();
System.out.println(list2.size());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}