首先對於ehcache作個簡單介紹:html
EhCache 是一個純Java的進程內緩存框架,具備快速、精幹等特色,是Hibernate中默認的CacheProvider。前端
下圖是 Ehcache 在應用程序中的位置:java
主要的特性有:算法
1. 快速.
2. 簡單.
3. 多種緩存策略
4. 緩存數據有兩級:內存和磁盤,所以無需擔憂容量問題
5. 緩存數據會在虛擬機重啓的過程當中寫入磁盤
6. 能夠經過RMI、可插入API等方式進行分佈式緩存
7. 具備緩存和緩存管理器的偵聽接口
8. 支持多緩存管理器實例,以及一個實例的多個緩存區域
9. 提供Hibernate的緩存實現
10. 等等spring
在線API doc:http://www.ostools.net/apidocs/apidoc?api=ehcache2.5.2api
咱們的一個接受中心項目主要接收數據、處理數據,不作前端展現 ;採用的緩存框架是ehcache。緩存
在啓動jvm的時候,將須要緩存的數據交給ehcache管理,ehcache有兩種存儲方式;磁盤和內存;ehcache既能夠單獨使用,亦能夠配合其餘框架使用,如用spring實現ehcache,配合hibernate、mybatis使用;mybatis
使用CacheManager 建立並管理Cache ,建立CacheManager有4種方式:框架
a1. 使用默認配置文件建立 (默認配置文件:ehcache.xml)jvm
java代碼:
CacheManager manager = CacheManager.create();
a2;使用指定配置文件建立
java代碼:
CacheManager manager = CacheManager.create("src/rescoure/config/ehcache.xml");
a3:從classpath中找尋配置文件並建立
java代碼:
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
a4:經過輸入流建立
java代碼:
InputStream fis = new FileInputStream(new File("src/rescoure/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
}catch(Exception e){
log.error(e.getMessage(),e);
} finally {
fis.close();
}
:卸載CacheManager ,關閉Cache
java代碼:
manager.shutdown();
具體的API以下所示;
Constructor Summary | |
---|---|
CacheManager() Constructor. |
|
CacheManager(Configuration configuration) An constructor for CacheManager, which takes a configuration object, rather than one created by parsing an ehcache.xml file. |
|
CacheManager(InputStream configurationInputStream) An ordinary constructor for CacheManager. |
|
CacheManager(String configurationFileName) An ordinary constructor for CacheManager. |
|
CacheManager(URL configurationURL) An ordinary constructor for CacheManager |
默認的配置文件: ehcache配置(ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="PersonCache">
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="1200"
timeToLiveSeconds="1200"
overflowToDisk="false">
</defaultCache>
<cache name="sampleCache1"
maxElementsInMemory="100"
maxElementsOnDisk="0"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="0">
<terracotta/>
</cache>
</cache>
</ehcache>
cache參數詳解:
name:指定區域名
maxElementsInMemory :緩存在內存中的最大數目
maxElementsOnDisk:緩存在磁盤上的最大數目
eternal :緩存是否持久
overflowToDisk : 硬盤溢出數目
timeToIdleSeconds :當緩存條目閒置n秒後銷燬
timeToLiveSeconds :當緩存條目存活n秒後銷燬
memoryStoreEvictionPolicy:緩存算法,有LRU(默認)、LFU、FIFO
添加Cache到CacheManegr中也有多種方式;
b一、取得配置文件中預先 定義的name="sampleCache1"> 設置,經過CacheManager生成一個Cache
java代碼:
CacheManager manager = CacheManager.create(); //建立CacheManager
Cache cache = manager.getCache("sampleCache1"); //使用建立的CacheManager建立一個cache
b二、置一個名爲sampleCache2的新cache,test屬性爲默認
java代碼:
CacheManager manager = CacheManager.create();
manager.addCache("sampleCache2");
b三、設置一個名爲sampleCache3的新cache,並定義其屬性
java代碼:
CacheManager manager = CacheManager.create();
Cache cache = new Cache("sampleCache3", 1, true, false, 5, 2);
manager.addCache(cache);
相應的API以下:
net.sf.ehcache
Class CacheManager
java.lang.Object net.sf.ehcache.CacheManag
void |
addCache(Cache cache) Adds a Cache to the CacheManager. |
void |
addCache(Ehcache cache) Adds an Ehcache to the CacheManager. |
void |
addCache(String cacheName) Adds a Ehcache based on the defaultCache with the given name. |
net.sf.ehcache
Class Cache
java.lang.Object net.sf.ehcache.Cache
Cache(String name, int maxElementsInMemory, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds) 1.0 Constructor. |
Cache(String name, int maxElementsInMemory, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds, boolean diskPersistent, long diskExpiryThreadIntervalSeconds) 1.1 Constructor. |
Cache(String name, int maxElementsInMemory, MemoryStoreEvictionPolicy memoryStoreEvictionPolicy, boolean overflowToDisk, String diskStorePath, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds, boolean diskPersistent, long diskExpiryThreadIntervalSeconds,RegisteredEventListeners registeredEventListeners) 1.2 Constructor |
其餘更高版本的方法。。。。
到此,咱們已經往CacheManage添加Cache完成,下面就是操做Cache中的元素,主要是增刪改查;
c一、增長元素:
java代碼:
Element element = new Element("key1", "value1");
cache.put(new Element(element); //將key爲key1,value爲value1的元素 放進cache中
c二、修改元素:
java代碼:
Element element = new Element("key1", "value2");
cache.replace(element); //若是key爲key1的元素存在cache中,就 將key爲key1,value爲value2的這個元素放進cache中,以前的key爲key1的元素被替換
c三、查詢元素:
java代碼;
//新增一個元素
Element element = new Element("key1", "value1");
cache.put(element); //將key爲key1,value爲value1的元素 放進cache中
//修改key爲key1的元素
cache.replace(new Element("key1", "value2"));
//查詢key爲key1的元素
Element element = cache.get("key1");
c四、刪除元素:
java代碼;
boolean isRemove = cache.remove("key1");
因此大概步驟爲:
第一步:生成CacheManager對象
第二步:生成Cache對象
第三步:向Cache對象裏添加由key,value組成的鍵值對的Element元素
net.sf.ehcache
Class Cache
Element |
get(Object key) Gets an element from the cache. |
Element |
get(Serializable key) Gets an element from the cache. |
Map<Object,Element> |
getAll(Collection<?> keys) Gets all the elements from the cache for the keys provided. |
int |
getSize() Gets the size of the cache. |
void |
put(Element element) Put an element in the cache. |
void |
put(Element element, boolean doNotNotifyCacheReplicators) Put an element in the cache. |
void |
putAll(Collection<Element> elements) Puts a collection of elements in the cache. |
boolean |
remove(Object key, boolean doNotNotifyCacheReplicators) Removes an Element from the Cache. |
booean |
remove(Serializable key) Removes an Element from the Cache. |
boolean |
remove(Serializable key, boolean doNotNotifyCacheReplicators) Removes an Element from the Cache. |
void |
removeAll() Removes all cached items. |
Element |
replace(Element element) Replace the cached element only if an Element is currently cached for this key |
boolean |
replace(Element old,Element element) Replace the cached element only if the current Element is equal to the supplied old Element. |
以上是ehcache的簡單應用,ehcache的優勢和缺點接下來將一一講解:
附:經常使用緩存技術說明: http://sishuok.com/forum/posts/list/275.html