EhCache 是一個純Java的進程內緩存框架,具備快速、精幹等特色,是Hibernate中默認的CacheProvider。java
看到官網上已經3.0了。web
我把它當作一個能夠存儲數據和讀取數據的存在。緩存其實就一個key-value的數據存儲工具。目前我使用過兩個方面。spring
都說鏈接數據庫的開銷很大,因此對數據變化較小的一部分能夠緩存起來,下次直接從緩存中取數據。(關於數據的安全性不曾考慮)。sql
在web後端開發過程當中,有些數據須要保存起來方便下次使用。好比短信的驗證碼。我以前都是在類中添加一個map類變量,而後存入map,下次取出或者銷燬。這樣作事能夠的,類實例化後。類變量就加載到內存中了,能夠存儲數據。但有個問題,關於類的建立和銷燬的聲明週期問題。不能確保他何時去銷燬。或者說,這一塊我瞭解有限,還沒研究到。數據庫
所以,放到緩存中就能夠了。緩存能夠設置數據的大小,失效時間。後端
(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>
-------------------------------------------------------------------------------------------------------
<!-- <defaultCache
maxElementsInMemory="10000" 內存中最大緩存對象數
eternal="false" Element是否永久有效,一但設置了,timeout將不起做用
timeToIdleSeconds="120" 設置對象在失效前的容許閒置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。
timeToLiveSeconds="120" 設置對象在失效前容許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
overflowToDisk="true" 設置當內存中緩存達到maxInMemory 限制時元素是否可寫到磁盤上
maxElementsOnDisk="10000000" 磁盤緩存大小默認是沒有限制的,不過可經過maxElementsOnDisk來指定。當磁盤緩存達到maxElementsOnDisk指定的值時,Ehcache會清理磁盤中的緩存使用默認策略是LFU(使用頻率最低)。
diskPersistent="false" 在VM重啓的時候是否持久化磁盤緩存,默認是false。
diskExpiryThreadIntervalSeconds="120" 磁盤緩存的清理線程運行間隔,默認是120秒.
memoryStoreEvictionPolicy="LRU" 當內存緩存達到最大,有新的element加入的時候,移除緩存中element的策略。默認是LRU,可選的有LFU和FIFO可對緩存中的element配置諸如監聽器和加載器。
/> -->
<!-- <cache
name="sampleCache" cache惟一標識
maxElementsInMemory="5" 內存中最大緩存對象數
maxElementsOnDisk="100" 磁盤緩存大小默認是沒有限制的,不過可經過maxElementsOnDisk來指定。當磁盤緩存達到maxElementsOnDisk指定的值時,Ehcache會清理磁盤中的緩存使用默認策略是LFU(使用頻率最低)。
eternal="false" Element是否永久有效,一但設置了,timeout將不起做用
timeToIdleSeconds="2" 設置對象在失效前的容許閒置時間(單位:秒)。僅當eternal=false對象不是永久有效時使用,可選屬性,默認值是0,也就是可閒置時間無窮大。
timeToLiveSeconds="2" 設置對象在失效前容許存活時間(單位:秒)。最大時間介於建立時間和失效時間之間。僅當eternal=false對象不是永久有效時使用,默認是0.,也就是對象存活時間無窮大。
overflowToDisk="true" 設置當內存中緩存達到maxInMemory 限制時元素是否可寫到磁盤上
/> -->
ehcache.xml配置參數說明:
-------------------------------------------------------------------------------------------------------
2.EhcacheDemo.java文件
Ehcache會自動加載classpath根目錄下名爲ehcache.xml文件。
EhcacheDemo的工做步驟以下:
在EhcacheDemo中,咱們引用ehcache.xml聲明的名爲helloworld的緩存來建立對象;
而後咱們用一個鍵值對來實例化對象;
將對象添加到;
而後用的get方法獲取對象。
----------------------------------------------------------------------------------------CacheElementElementCacheCacheElement
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的生命週期。
ehcache是一個用Java實現的使用簡單,高速,實現線程安全的緩存管理類庫,ehcache提供了用內存,磁盤文件存儲,以及分佈式存儲方式等多種靈活的cache管理方案。同時ehcache做爲開放源代碼項目,採用限制比較寬鬆的Apache License V2.0做爲受權方式,被普遍地用於Hibernate, Spring,Cocoon等其餘開源系統。
Ehcache的類層次模型主要爲三層,最上層的是CacheManager,他是操做Ehcache的入口。咱們能夠經過CacheManager.getInstance()得到一個單個的CacheManager,或者經過CacheManager的構造函數建立一個新的CacheManager。每一個CacheManager都管理着多個Cache。而每一個Cache都以一種類Hash的方式,關聯着多個Elemenat。而Element則是咱們用於存放要緩存內容的地方。
ehcache的刷新策略 ehcache的刷新策略是當緩存在放入的時候記錄一個放入時間,它是用Lazy Evict的方式,在取的時候同設置的TTL比較
ehcache緩存的3種清空策略: 1 FIFO,先進先出 2 LFU,最少被使用,緩存的元素有一個hit屬性,hit值最小的將會被清出緩存。 3 LRU,最近最少使用的,緩存的元素有一個時間戳,當緩存容量滿了,而又須要騰出地方來緩存新的元素的時候,那麼現有緩存元素中時間戳離當前時間最遠的元素將被清出緩存。
事件處理 能夠爲CacheManager添加事件監聽,當對CacheManager增刪Cache時,事件處理器將會獲得通知。要配置事件處理,須要經過ehcache的配置文件來完成。 能夠爲Cache添加事件監聽,當對Cache增刪Element時,事件處理器將會獲得通知。要配置事件處理,須要經過ehcache的配置文件來完成。