轉自:自定義javaweb緩存設計

主要是針對單機應用下的緩存設計:

緩存實體類:

package com.whty.transform.common.cache;

/**
 * Created by Administrator on 2018/10/15 0015.
 */
public class EntityCache {
    /**
     * 保存的數據
     */
    private  Object datas;

    /**
     * 設置數據失效時間,爲0表示永不失效,過期時間單位s
     */
    private  long timeOut;

    /**
     * 最後刷新時間
     */
    private  long lastRefeshTime;

    public EntityCache(Object datas, long timeOut, long lastRefeshTime) {
        this.datas = datas;
        this.timeOut = timeOut;
        this.lastRefeshTime = lastRefeshTime;
    }
    public Object getDatas() {
        return datas;
    }
    public void setDatas(Object datas) {
        this.datas = datas;
    }
    public long getTimeOut() {
        return timeOut;
    }
    public void setTimeOut(long timeOut) {
        this.timeOut = timeOut;
    }
    public long getLastRefeshTime() {
        return lastRefeshTime;
    }
    public void setLastRefeshTime(long lastRefeshTime) {
        this.lastRefeshTime = lastRefeshTime;
    }
}

說明:

datas:存放緩存的數據

timeOut:設置該緩存是否過期

lastRefeshTime:緩存存放的時間,默認是系統當前時間,後面跑監聽銷燬使用

 

緩存接口:

package com.whty.transform.common.cache;

import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2018/10/15 0015.
 */
public interface ICacheManager {
    /**
     * 存入緩存
     * @param key
     * @param cache
     */
    void putCache(String key, EntityCache cache);

    /**
     * 存入緩存
     * @param key
     * @param datas
     */
    void putCache(String key, Object datas, long timeOut);

    /**
     * 獲取對應緩存
     * @param key
     * @return
     */
    EntityCache getCacheByKey(String key);

    /**
     * 獲取對應緩存
     * @param key
     * @return
     */
    Object getCacheDataByKey(String key);

    /**
     * 獲取所有緩存
     * @return
     */
    Map<String, EntityCache> getCacheAll();

    /**
     * 判斷是否在緩存中
     * @param key
     * @return
     */
    boolean isContains(String key);

    /**
     * 清除所有緩存
     */
    void clearAll();

    /**
     * 清除對應緩存
     * @param key
     */
    void clearByKey(String key);

    /**
     * 緩存是否超時失效
     * @param key
     * @return
     */
    boolean isTimeOut(String key);

    /**
     * 獲取所有key
     * @return
     */
    Set<String> getAllKeys();
}

 

緩存接口實現類:

package com.whty.transform.common.cache;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2018/10/15 0015.
 */
public class CacheManagerImpl implements ICacheManager {
    private static Map<String, EntityCache> caches = new ConcurrentHashMap<String, EntityCache>();
    private static CacheManagerImpl instance = null;

    private CacheManagerImpl() {}

    public static synchronized CacheManagerImpl getInstance(){
        if(instance == null){
            instance = new CacheManagerImpl();
        }
        return instance;
    }
    /**
     * 存入緩存
     * @param key
     * @param cache
     */
    public void putCache(String key, EntityCache cache) {
        caches.put(key, cache);
    }

    /**
     * 存入緩存
     * @param key
     * @param datas
     */
    public void putCache(String key, Object datas, long timeOut) {
        timeOut = timeOut > 0 ? timeOut : 0L;
        putCache(key, new EntityCache(datas, timeOut, System.currentTimeMillis()));
    }

    /**
     * 獲取對應緩存
     * @param key
     * @return
     */
    public EntityCache getCacheByKey(String key) {
        if (this.isContains(key)) {
            return caches.get(key);
        }
        return null;
    }

    /**
     * 獲取對應緩存
     * @param key
     * @return
     */
    public Object getCacheDataByKey(String key) {
        if (this.isContains(key)) {
            return caches.get(key).getDatas();
        }
        return null;
    }

    /**
     * 獲取所有緩存
     * @return
     */
    public Map<String, EntityCache> getCacheAll() {
        return caches;
    }

    /**
     * 判斷是否在緩存中
     * @param key
     * @return
     */
    public boolean isContains(String key) {
        return caches.containsKey(key);
    }

    /**
     * 清除所有緩存
     */
    public void clearAll() {
        caches.clear();
    }

    /**
     * 清除對應緩存
     * @param key
     */
    public void clearByKey(String key) {
        if (this.isContains(key)) {
            caches.remove(key);
        }
    }

    /**
     * 緩存是否超時失效
     * @param key
     * @return
     */
    public boolean isTimeOut(String key) {
        if (!caches.containsKey(key)) {
            return true;
        }
        EntityCache cache = caches.get(key);
        long timeOut = cache.getTimeOut();
        long lastRefreshTime = cache.getLastRefeshTime();
        if (timeOut != 0 && System.currentTimeMillis() - lastRefreshTime >= timeOut) {
            return true;
        }
        return false;
    }

    /**
     * 獲取所有key
     * @return
     */
    public Set<String>  getAllKeys() {
        return caches.keySet();
    }
}

 

說明:使用單例模式設計獲取實例。

 

 緩存監聽類:

package com.whty.transform.web.listener;


import com.whty.transform.common.cache.CacheManagerImpl;

/**
 * Created by Administrator on 2018/10/15 0015.
 */
public class CacheListener {
    private CacheManagerImpl cacheManagerImpl;

    public CacheListener(CacheManagerImpl cacheManagerImpl) {
        this.cacheManagerImpl = cacheManagerImpl;
    }

    public void startListen() {
        new Thread() {
            public void run() {
                while (true) {
                    for (String key : cacheManagerImpl.getAllKeys()) {
                        if (cacheManagerImpl.isTimeOut(key)) {
                            cacheManagerImpl.clearByKey(key);
                        }
                    }
                }
            }
        }.start();

    }
}

 

 實時清空緩存過期的對象,注意:該監聽在javaee的servlerContext上下文監聽下觸發比較好。

 

測試:

CacheManagerImpl.getInstance().putCache(CommonConstants.TRANSFORM_IP_ADDRESS, ipAdds.substring(0,ipAdds.length() - 1), 0);

可以自行測試,結束!感謝開源! 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章