1. 程式人生 > >Ehcache學習筆記(2)--自定義ehcache工具類

Ehcache學習筆記(2)--自定義ehcache工具類

二:自定義EhcacheUtils
1、CacheUtils

package cn.kexq.commons.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CacheUtils {
    @Autowired
    private CacheManager cacheManager;

    /**
     * 獲取Ehcache物件
     * @param cacheName
     * @return
     */
    public Cache getEhcache(String cacheName){
        return cacheManager.getCache(cacheName);
    }

    /**
     * 獲取快取
     * @param cacheName
     * @param key
     * @return
     */
    public Object getCache(String cacheName,String key){
        Cache cache = cacheManager.getCache(cacheName);
        if(cache==null){
            return null;
        }
        return cache.get(key).getObjectValue();
    }

    /**
     * 儲存快取--沒有則建立一個
     * @param cacheName
     * @param key
     * @param value
     */
    public void putCache(String cacheName,String key,Object value){
        Cache cache = cacheManager.getCache(cacheName);
        if(cache==null){
            System.out.println("cache is null...");
            cacheManager.addCache(cacheName);
            cache = cacheManager.getCache(cacheName);
            cache.put(new Element(key,value));
        }
        cache.put(new Element(key,value));
    }

    /**
     * 刪除快取
     * @param cacheName
     */
    public void removeCache(String cacheName,String key){
        Cache cache = cacheManager.getCache(cacheName);
        if(cache!=null){
            cache.remove(key);
        }else{
            System.out.println("cache is null...");
        }
    }

    /**
     * 替換快取
     * @param cacheName
     * @param key
     * @param value
     */
    public void replaceCache(String cacheName,String key,String value){
        Cache cache = cacheManager.getCache(cacheName);
        if(cache!=null){
            cache.replace(new Element(key,value));
        }
    }

    /**
     * 關閉快取
     */
    public void shutDownCache(){
        cacheManager.shutdown();
    }


    public CacheManager getCacheManager() {
        return cacheManager;
    }

    public void setCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
    }

}

2、TestDemo

    @Resource
    private CacheUtils cacheUtils;
    
	@RequestMapping("cacheTest.do")
    public String getCache(){
        try {
            System.out.println("----------------getCache---------------");
            User user = (User) cacheUtils.getCache("demoCache", "ke");
            System.out.println(user);

            //儲存一個快取資料,若該快取不存在則建立一個新的快取
            cacheUtils.putCache("testCache", "kexq", "this is myCache test ...");
            String s = (String)cacheUtils.getCache("testCache","kexq");
            System.out.println(s);

            //預設快取配置引數為defaultCache
            Cache cache = cacheUtils.getEhcache("testCache");
            System.out.println(cache.getMemoryStoreEvictionPolicy().getName());;
            System.out.println(cache);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "index";
    }