package com.yanzuoguang.util.cache; import com.yanzuoguang.util.helper.StringHelper; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * 内存缓存 * * @param <T> * @author 颜佐光 */ public class MemoryCache<T> { /** * 缓存的对象 */ private Map<String, MemoryCacheItem<T>> cache = new ConcurrentHashMap<>(); /** * 清除时间 */ private int clearSecond; /** * 是否自动清除缓存 */ private boolean isAutoClear; /** * 读取时是否激活 */ private boolean isGetActive; /** * 构造函数,不会自动清除对象 */ public MemoryCache() { this(false, 0); } /** * 构造函数 * * @param clearSecond 清除缓存的时间 */ public MemoryCache(int clearSecond) { this(true, clearSecond); } /** * 构造函数 * * @param isAutoClear 是否自动清除 * @param clearSecond 清除缓存的时间 */ public MemoryCache(boolean isAutoClear, int clearSecond) { this.isAutoClear = isAutoClear; this.clearSecond = clearSecond; this.isGetActive = false; if (isAutoClear) { MemoryCacheCenter.CLEAR_LIST.add(this); } } public boolean isGetActive() { return isGetActive; } public void setGetActive(boolean getActive) { isGetActive = getActive; } public int getClearSecond() { return clearSecond; } public void setClearSecond(int clearSecond) { this.clearSecond = clearSecond; } /** * 写入数据 * * @param key 关键字 * @param data 数据 */ public synchronized T put(String key, T data) { if (this.isAutoClear && this.clearSecond < 1) { return data; } MemoryCacheItem<T> item = cache.containsKey(key) ? cache.get(key) : null; if (item == null) { item = new MemoryCacheItem<T>(); cache.put(key, item); } item.setData(data); if (StringHelper.isEmpty(item.getName())) { item.setName(key); cache.put(key, item); } return item.getData(); } /** * 读取数据 * * @param key 关键字 */ public synchronized T get(String key) { if (this.isAutoClear && this.clearSecond < 1) { return null; } MemoryCacheItem<T> item = cache.get(key); if (item == null) { return null; } this.getActive(item); return item.getData(); } /** * 读取数据 * * @param key 关键字 * @param defData 默认数据 * @return */ public synchronized T get(String key, T defData) { if (this.isAutoClear && this.clearSecond < 1) { return defData; } MemoryCacheItem<T> item = cache.getOrDefault(key, new MemoryCacheItem<T>()); if (StringHelper.isEmpty(item.getName())) { item.setName(key); item.setData(defData); cache.put(key, item); } else { if (defData instanceof MemoryCache) { ((MemoryCache) defData).close(); } } this.getActive(item); return item.getData(); } /** * 在获取时重新激活数据 */ private void getActive(MemoryCacheItem<T> item) { if (isGetActive) { item.active(); } } /** * 删除一个数据 * * @param key 关键字 */ public synchronized T remove(String key) { MemoryCacheItem<T> item = cache.remove(key); if (item == null) { return null; } T data = item.getData(); if (data instanceof MemoryCache) { ((MemoryCache) data).close(); } return data; } /** * 清除所有的缓存 */ public synchronized void clear() { Object[] keys = cache.keySet().toArray(); for (Object keyObject : keys) { // 关键字 String key = keyObject.toString(); this.remove(key); } } /** * 清除超时的数据 */ public synchronized void clearTimeout() { if (this.clearSecond < 0) { return; } Object[] keys = cache.keySet().toArray(); Date now = new Date(); for (Object keyObject : keys) { // 关键字 String key = keyObject.toString(); // 获取到数据 MemoryCacheItem<T> itemValue = this.cache.get(key); if (itemValue == null) { continue; } // 判断是否过期 if (now.getTime() - itemValue.getDate().getTime() > this.clearSecond * 1000) { this.remove(key); } } } /** * 将缓存对象从缓存对象中清除 */ public synchronized void close() { this.clear(); MemoryCacheCenter.CLEAR_LIST.remove(this); } /** * 获取所有的关键字 * * @return */ public synchronized String[] getKeys() { String[] items = new String[this.cache.size()]; return this.cache.keySet().toArray(items); } /** * 析构函数 * * @throws Throwable */ @Override protected void finalize() { this.close(); } }