【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
相关文章
相关标签/搜索