echache緩存的簡單使用方法


1、需要echache的jar包

2、需要配置文件ehcache.xml和ehcache.xsd,主要是在ehcache.xml中進行配置

3、修改配置文件ehcache.xml  ,例如添加配置如下:

    <cache name="memoryCache"
            maxElementsInMemory="500" <!-- 最大緩存數量 -->
            eternal="true"  <!-- 在內存中永久存在,由於此處設置為true,所以以下兩個參數無效 -->
            timeToIdleSeconds="3600"  
            timeToLiveSeconds="7200"
            overflowToDisk="false"/>  
            
     <cache name="reportCache"
            maxElementsInMemory="500"
            eternal="false"  <!-- 不在內存中永久存在 -->
            timeToIdleSeconds="3600"  <!-- 一個小時之內不再用到這個緩存就清理 -->
            timeToLiveSeconds="7200"  <!-- 兩個小時以后不管是否用到這個緩存都會被清理 -->
            overflowToDisk="false"/>  <!-- 當超過500是不會寫入磁盤 -->

     <cache name="diskCache"   <!-- 以上兩種都是內存緩存,這里配置的是磁盤緩存 -->
            maxElementsInMemory="10000"
            overflowToDisk="true"  <!-- 當內存中超過10000, 就寫入磁盤 -->
            eternal="false"
            memoryStoreEvictionPolicy="LRU"  <!-- 這里配置的是清除緩存時的策略 -->
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="600"
            timeToIdleSeconds="3600"
            timeToLiveSeconds="100000"
            diskPersistent="false" />  <!-- 不是磁盤永存 -->

這里就建立了三種緩存形式

4、可以建立一個或者多個獨立的類,用於對應配置文件中的配置,例如:

package com.cetc32.cache;

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

public class ReportCache {
    private static ReportCache reportCache = null;
    private static Cache cache = null;
    
    //實現單例模式
    public static ReportCache getInstance() {
        if(reportCache == null) {
            reportCache = new ReportCache();
        } 
        return reportCache;
    }
    
    //private Cache cache;

    public ReportCache() {
        String path = this.getClass().getResource("/config/ehcache.xml").getFile();

        CacheManager manager = CacheManager.create(path);
        cache = manager.getCache("reportCache");
    }

    /**
     * 設置緩存
     * @param key
     * @param o
     */
    public void setReportCache(String key, Object o) {

        Element element = new Element(key, o);
        cache.put(element);

    }

    /**
     * 從緩存中獲得結果
     * @param key
     * @return
     */
    public Object getReportCache(String key) {
        Element aa = cache.get(key);
        Object r = null;
        if (aa != null) {
            r = aa.getObjectValue();
        }
        return r;

    }
    
    /**
     * 清除某個緩存
     * @param key
     */
    public boolean removeReportCache(String key) {
        return cache.remove(key);
    }

    /**
     * 清空全部緩存
     */
    public void removeAllReportCache() {
        cache.removeAll();
    }
    
    /**
     * @return the cache
     */
    public Cache getCache() {
        return cache;
    }



}

這里采用的是單例模式,應用中一個實例即可

6、在程序中使用 ReportCache reportCache = ReportCache.getInstance(); 獲取實例就可以進行緩存操作了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM