单独配置 Ehcache

package com.shy.ehcache;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EhcacheDemo {
    
    public static void main(String[] args) {
        
        //加载配置文件路径为:src/config/ehcache.xml
        CacheManager manager = CacheManager.create("src/config/ehcache.xml");
        //获取全部缓存名称
        String[] names = manager.getCacheNames();
        //打印缓存名称
        for(String name : names){
            System.out.println("ehcache names = "+name);
        }
        //获取指定的cache
        Cache cache = manager.getCache(names[0]);
        //创建一个内存元素
        Element e = new Element("sayHello", "hello");
        //将元素放入内存
        cache.put(e);
        //从内存读取
        System.out.println(cache.get("sayHello"));
        
    }
    
}

 

posted @ 2017-02-21 10:07  MIC2016  阅读(200)  评论(0编辑  收藏  举报