知識點:在java項目中,使用ehcache緩存數據
參考博客:http://www.cnblogs.com/jingmoxukong/p/5975994.html
(1)概述
Ehcache是一個純Java的進程內緩存框架,具有快速‘精干等特點。
本文基於2.10.X以上版本
(2)在pom.xml添加相關包依賴
<!-- ehcache緩存包-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!-- spring-context-support包含有Spring對於緩存功能的抽象封裝接口-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
(3)HelloWorld實例使用Ehcache緩存
1.在classpath下添加ehcache.xml配置文件,添加一個名為helloworld的緩存
--------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- 磁盤緩存位置 -->
<diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默認緩存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<!-- helloworld緩存 -->
<cache name="helloworld"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="5"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
-------------------------------------------------------------------------------------------------------
ehcache.xml配置參數說明:
- name:緩存名稱。
- maxElementsInMemory:緩存最大個數。
- eternal:緩存中對象是否為永久的,如果是,超時設置將被忽略,對象從不過期。
- timeToIdleSeconds:置對象在失效前的允許閑置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閑置時間無窮大。
- timeToLiveSeconds:緩存數據的生存時間(TTL),也就是一個元素從構建到消亡的最大時間間隔值,這只能在元素不是永久駐留時有效,如果該值是0就意味着元素可以停頓無窮長的時間。
- maxEntriesLocalDisk:當內存中對象數量達到maxElementsInMemory時,Ehcache將會對象寫到磁盤中。
- overflowToDisk:內存不足時,是否啟用磁盤緩存。
- diskSpoolBufferSizeMB:這個參數設置DiskStore(磁盤緩存)的緩存區大小。默認是30MB。每個Cache都應該有自己的一個緩沖區。
- maxElementsOnDisk:硬盤最大緩存個數。
- diskPersistent:是否在VM重啟時存儲硬盤的緩存數據。默認值是false。
- diskExpiryThreadIntervalSeconds:磁盤失效線程運行時間間隔,默認是120秒。
- memoryStoreEvictionPolicy:當達到maxElementsInMemory限制時,Ehcache將會根據指定的策略去清理內存。默認策略是LRU(最近最少使用)。你可以設置為FIFO(先進先出)或是LFU(較少使用)。
- clearOnFlush:內存數量最大時是否清除。
-------------------------------------------------------------------------------------------------------
2.EhcacheDemo.java文件
Ehcache會自動加載classpath根目錄下名為ehcache.xml文件。
EhcacheDemo的工作步驟如下:
在EhcacheDemo中,我們引用ehcache.xml聲明的名為helloworld的緩存來創建Cache
對象;
然后我們用一個鍵值對來實例化Element
對象;
將Element
對象添加到Cache
;
然后用Cache
的get方法獲取Element
對象。
----------------------------------------------------------------------------------------
package com.agesun.attendance.web.controller;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class EhcacheDemo {
public static void main(String[] args){
// Create a cache manager 創建緩存管理者
final CacheManager cacheManager=new CacheManager();
// create the cache called "helloworld" 引用ehcache.xml申明的的名為helloworld的緩存創建Cache對象
final Cache cache=cacheManager.getCache("helloworld");
// create a key to map the data to
final String key="greeting";
// Create a data element
final Element putGreeting=new Element(key,"Hello,World!");
// Put the element into the data store //將map對象放到cache緩存里
cache.put(putGreeting);
// Retrieve the data element //從cache對象中獲得到元素
final Element getGreeting=cache.get(key);
// Retrieve the data element
System.out.println(getGreeting.getObjectValue());
}
}
----------------------------------------------------------------------------------
輸出:
(4)Ehcache基本操作
Element、Cache、cacheManager是Ehcacle最重要的API
Element:緩存的元素,維護着一個鍵值對
Cache:是Ehcache的核心類,他有多個Element,並被CacheManager管理,它實現了對緩存的邏輯行為
CacheManager:Cache的容器對象, 並管理着Cache的生命周期。