ehcache的xml配置和基本使用

247 阅读1分钟

前言

ehcache是一个由java实现的轻量级的缓存工具包,提供内存和磁盘的存贮,支持分布式,多种淘汰算法,LRU,LFU,FIFO,可以与很多框架整合。

依赖 net.sf.ehcache ehcache-core 2.5.0

xml文件配置

    <!--持久化磁盘路径-->
    <diskStore path="java.io.tmpdir"/>



    <!--默认缓存设置-->
    <defaultCache maxElementsInMemory="1000"
                   eternal="false"
                   timeToIdleSeconds="3600"
                   timeToLiveSeconds="0"
                   overflowToDisk="true"
                   maxElementsOnDisk="10000"
                   diskPersistent="false"
                   diskExpiryThreadIntervalSeconds="120"
                   memoryStoreEvictionPolicy="FIFO"
    />
     

    <!--
        <cache     name 缓存名唯一标识
                   maxElementsInMemory="1000" 内存中最大缓存对象数
                   eternal="false" 是否永久缓存
                   timeToIdleSeconds="3600" 缓存清除时间 默认是0 即永不过期
                   timeToLiveSeconds="0" 缓存存活时间 默认是0 即永不过期
                   overflowToDisk="true" 缓存对象达到最大数后,将其写入硬盘
                   maxElementsOnDisk="10000"  磁盘最大缓存数
                   diskPersistent="false" 磁盘持久化
                   diskExpiryThreadIntervalSeconds="120" 磁盘缓存的清理线程运行间隔
                   memoryStoreEvictionPolicy="FIFO" 缓存清空策略
                   FIFO 先进先出
                   LFU  less frequently used  最少使用
                   LRU  least recently used 最近最少使用
    />
    -->


    <cache name="testCache"
           maxEntriesLocalHeap="2000"
           eternal="false"
           timeToIdleSeconds="3600"
           timeToLiveSeconds="0"
           overflowToDisk="false"
           statistics="true"
           memoryStoreEvictionPolicy="FIFO">

    </cache>


</ehcache>

用法 import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import java.net.URL;

public class EhcacheUtil {

    private static final String default_path = "/ehcache.xml";

    private URL url;

    private CacheManager manager;

    public CacheManager getManager() {
        return manager;
    }

    public void setManager(CacheManager manager) {
        this.manager = manager;
    }

    private EhcacheUtil() {
        url = getClass().getResource(default_path);
        manager = CacheManager.create(url);
        manager.addCacheIfAbsent("default_cache");
    }


    private EhcacheUtil(String path) {
        url = getClass().getResource(path);
        System.out.println(url.getPath());
        manager = CacheManager.create(url);
        manager.addCacheIfAbsent("default_cache");
    }

    public void put(String cacheName, String key, Object value) {
        Cache cache = manager.getCache(cacheName);
        Element element = new Element(key, value);
        cache.put(element);
    }

    public Object get(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        Element element = cache.get(key);
        return element == null ? null : element.getObjectValue();
    }

    public Cache get(String cacheName) {
        return manager.getCache(cacheName);
    }

    public void remove(String cacheName, String key) {

        Cache cache = manager.getCache(cacheName);
        cache.remove(key);
    }

    public static void main(String[] args){
        EhcacheUtil ehcacheUtil = new EhcacheUtil("/ehcache.xml");
        ehcacheUtil.put("default_cache","key","value");
        String result = (String)ehcacheUtil.get("default_cache","key");
        System.out.println(result);
        //System.out.println(EhcacheUtil.class.getResource(""));
    }

}

CacheManager 管理器管理cache对象,cache对象就是我们上面xml文件配置,对应的name。 ———————————————— 版权声明:本文为CSDN博主「forwardMyLife」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/lucky_ly/ar…