【Cache】- Ehcache 基本操做

Ehcache 結構層次: CacheManager、Cache、Elementjava

這裏寫圖片描述

  • CacheManager:緩存管理器,主要負責Cache對象的建立、回收等
  • Cache:主要負責Element元素的統一管理
  • Element:元素,能夠理解成緩存數據的小單元,每一個單元都經過Map的形式進行存儲,是緩存數據的實際攜帶者

基礎案例:windows

package com.zhiwei.ehcache;

import java.util.List;
import java.util.Map;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * CacheManager:緩存管理器:負責Cache的建立、銷燬、回收等操做:通常只對應一個ehcache.xml文件,避免衝突
 * Cache:緩存對象:管理Element對象:一個cache對應多個element
 * element:實際緩存數據的攜帶者
 * 
 * 注意:Ehcache3.0以上的版本的使用方式和這裏使用的2.1有些區別
 * @author Yang Zhiwei
 */

public class MainTest {
	
    @SuppressWarnings("unchecked")
	public static void main(String[] args) {
    	
    	//建立緩存管理器的單例對象
        CacheManager manager = CacheManager.newInstance("src/com/zhiwei/ehcache/ehcache.xml");
        
          /**使用默認配置建立緩存管理器
           *CacheManager defauleManager=manager.create();
           *System.out.println(defauleManager.getActiveConfigurationText());
          */
        
        //緩存管理器獲取Cache對象,注意Cache對象須要在ehcaceh.xml進行配置
        Cache cache = manager.getCache("TestCache");
        System.out.println("TestCache內存回收策略:"+cache.getMemoryStoreEvictionPolicy().getName()); //獲取內存回收策略
        
        //建立攜帶數據的Element對象:若是某些參數指明則使用默認配置
        Element element01 = new Element("key01", "This is my first cache data!");
        Element element02 = new Element("key02","This is my second cache data!");
        System.out.println("默認緩存空閒時間:"+element01.getTimeToIdle());
       
        //將數據存入緩存
        cache.put(element01);
        cache.put(element02);
        
        //獲取element01緩存元素的k-v
        Object key=  element01.getObjectKey();
        Object value = element01.getObjectValue();
        System.out.println("element01:"+key+"--"+value);
       
       //Cache管理Element對象
       Element element=cache.get("key01");
       System.err.println("自定義緩存空閒時間:"+element.getTimeToIdle()); 
       
       List<String> keys=cache.getKeys();
       System.out.println("cache緩存對象的keys:"+keys);
       //經過指定的key集合獲取緩存數據對象
       Map<Object,Element> map=cache.getAll(keys);
       System.out.println(map);
       
       cache.flush();  //緩存刷新:將數據寫入本地磁盤
     
       System.out.println("緩存元素個數:"+cache.getSize());  //獲取緩存的數據塊個數
       
        cache.dispose(); //釋放緩存cache管理的緩存數據塊
        manager.removeAllCaches();  //緩存管理器清空全部緩存cache對象
        manager.shutdown();   //關閉緩存管理器
    }
}

ehcache配置文件:緩存

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!--磁盤緩存本地目錄:JVM系統變量
    windows不一樣系統的路徑可能不一致,win10的路徑:
    C:\Users\squirrel\AppData\Local\Temp
    (能夠經過System.getProperty("java.io.tmpdir")查看)
-->
    <diskStore path="java.io.tmpdir"/>
    
    <!-- 
      maxEntriesLocalHeap:對內存最大緩存個數:0標識無限制
      eternal:緩存對象是否永久有效:優先級比timeout高
      timeToIdleSeconds:空閒時間設置,過時直接銷燬
      timeToLiveSeconds:緩存對象存活期
      maxEntriesLocalDisk:磁盤中的最大對象數:0標識無限制
      diskExpiryThreadIntervalSeconds:緩存檢測事件間隔,定時處理已緩存的數據元素
      diskSpoolBufferSizeMB:磁盤緩存區大小
      memoryStoreEvictionPolicy:內存回收策略
        LRU:least recently used:最近最少使用
        LFU: least Frequently used:最少使用
        FIFO: First In First Out:先進先出:隊列形式
     -->
    <defaultCache
            maxEntriesLocalHeap="10000"
            eternal="false"
            timeToIdleSeconds="120"  
            timeToLiveSeconds="120"  
            diskSpoolBufferSizeMB="30"  
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
    
    <!-- 配置緩存對象Cache:cachaManaget經過緩存名稱獲取緩存對象 -->
    <cache name="TestCache"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           diskSpoolBufferSizeMB="20"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

結果:ui

TestCache內存回收策略:LFU
默認緩存空閒時間:0
element01:key01--This is my first cache data!
自定義緩存空閒時間:300
cache緩存對象的keys:[key02, key01]
{key02=[ key = key02, value=This is my second cache data!, version=1, hitCount=1, CreationTime = 1482130695701, LastAccessTime = 1482130695701 ], key01=[ key = key01, value=This is my first cache data!, version=1, hitCount=2, CreationTime = 1482130695701, LastAccessTime = 1482130695701 ]}
緩存元素個數:2
相關文章
相關標籤/搜索