jar包:
slf4j-api-1.6.1.jar
ehcache-core-2.5.2.jar
jar包下載下傳位址:http://download.csdn.net/download/wangshuxuncom/8528399
-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------
建立ehcache檔案:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--
name:緩存名稱。
maxElementsInMemory:緩存最大個數。
eternal:對象是否永久有效,一但設定了,timeout将不起作用。
timeToIdleSeconds:設定對象在失效前的允許閑置時間(機關:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,預設值是0,也就是可閑置時間無窮大。
timeToLiveSeconds:設定對象在失效前允許存活時間(機關:秒)。最大時間介于建立時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,預設是0.,也就是對象存活時間無窮大。
overflowToDisk:當記憶體中對象數量達到maxElementsInMemory時,Ehcache将會對象寫到磁盤中。
diskSpoolBufferSizeMB:這個參數設定DiskStore(磁盤緩存)的緩存區大小。預設是30MB。每個Cache都應該有自己的一個緩沖區。
maxElementsOnDisk:硬碟最大緩存個數。
diskPersistent:是否緩存虛拟機重新開機期資料 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盤失效線程運作時間間隔,預設是120秒。
memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache将會根據指定的政策去清理記憶體。預設政策是LRU(最近最少使用)。你可以設定為FIFO(先進先出)或是LFU(較少使用)。
clearOnFlush:記憶體數量最大時是否清除。
-->
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="1200"
overflowToDisk="true" />
<cache name="com.dict" maxElementsInMemory="150" eternal="false" timeToLiveSeconds="36000" timeToIdleSeconds="3600" overflowToDisk="true"/>
</ehcache>
建立資料字典:Dict.java,此處未貼代碼。
建立資料字典DAO接口:DictDao.java,此處未貼代碼。
建立資料字典DAO接口實作類:DictDaoImpl.java,此處未貼代碼。
建立EhcacheUtil.java類:
import java.net.URL;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhcacheUtil {
private static final String path = "/ehcache.xml";
private URL url;
private CacheManager manager;
private static EhcacheUtil ehCache;
private EhcacheUtil(String path) {
url = getClass().getResource(path);
manager = CacheManager.create(url);
}
public static EhcacheUtil getInstance() {
if (ehCache== null) {
ehCache= new EhcacheUtil(path);
}
return ehCache;
public void put(String cacheName, String key, Object value) {
Cache cache = manager.getCache(cacheName);
Element element = new Element(key, value);
cache.put(element);
public Object get(String cacheName, String key) {
Element element = cache.get(key);
return element == null ? null : element.getObjectValue();
public Cache get(String cacheName) {
return manager.getCache(cacheName);
public void remove(String cacheName, String key) {
cache.remove(key);
}
}
建立監聽類EhcacheInitListener.java:
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.shenzhen.management.dao.StudentDao;
import com.shenzhen.management.pojo.Dict;
public class EhcacheInitListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
//ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
DictDao dictDao = (DictDao) applicationContext.getBean("dictDao");
List<Dict> dicts = dictDao.getDicts();
EhcacheUtil ehcacheUtil = EhcacheUtil.getInstance();
for(Dict dict:dicts )
{
ehcacheUtil.put("com.dict", dict.getKey(), dict.getValue());
}
}