标签:
public class LiveCache<T> { // 缓存时间 private final int cacheMillis; // 缓存对象 private final T element; // 缓存对象创建时间 private final long createTime; public LiveCache(int cacheMillis, T element) { this.cacheMillis = cacheMillis; this.element = element; this.createTime = System.currentTimeMillis(); } // 获取缓存对象 public T getElement() { long currentTime = System.currentTimeMillis(); if(cacheMillis > 0 && currentTime - createTime > cacheMillis) { return null; } else { return element; } } // 获取缓存对象,忽略缓存时间有效性 public T getElementIfNecessary() { return element; } } public static void main(String[] args) { int cacheMilis = 1000 ; LiveCache<Object> liveCache = new LiveCache<>(cacheMilis, new Object()) ; liveCache.getElement() ; liveCache.getElementIfNecessary() ; }
@FunctionalInterface public interface LiveFetch<T> { // 刷新缓存接口 T fetch() ; } public class LiveManager<T> { // 缓存时间 private int cacheMillis; // 缓存对象 private LiveCache<T> liveCache; // 刷新缓存的对象 private LiveFetch<T> liveFetch ; private Logger logger = LoggerFactory.getLogger(LiveManager.class) ; // 刷新缓存开关 private boolean refresh = false ; public LiveManager(int cacheMillis, LiveFetch<T> liveFetch) { this.cacheMillis = cacheMillis ; this.liveFetch = liveFetch ; } /** * fetch cache ; if cache expired , synchronous fetch * @return */ public T getCache() { initLiveCache(); if(liveCache != null) { T t ; if((t= liveCache.getElement()) != null) { return t ; } else { t = liveFetch.fetch() ; if(t != null) { liveCache = new LiveCache<T>(cacheMillis, t) ; return t ; } } } return null ; } /** * fetch cache ; if cache expired , return old cache and asynchronous fetch * @return */ public T getCacheIfNecessary() { initLiveCache(); if(liveCache != null) { T t ; if((t= liveCache.getElement()) != null) { return t ; } else { refreshCache() ; return liveCache.getElementIfNecessary() ; } } return null ; } /** * init liveCache */ private void initLiveCache() { if(liveCache == null) { T t = liveFetch.fetch() ; if(t != null) { liveCache = new LiveCache<T>(cacheMillis, t) ; } } } /** * asynchronous refresh cache */ private void refreshCache() { if(refresh) return ; refresh = true ; try { Thread thread = new Thread(() -> { try { T t = liveFetch.fetch(); if (t != null) { liveCache = new LiveCache<>(cacheMillis, t); } } catch (Exception e){ logger.error("LiveManager.refreshCache thread error.", e); } finally { refresh = false ; } }) ; thread.start(); } catch (Exception e) { logger.error("LiveManager.refreshCache error.", e); } } } public class Test { public static void main(String[] args) { int cacheMilis = 1000 ; LiveManager<Object> liveManager = new LiveManager<>(cacheMilis,() -> new Test().t1()) ; liveManager.getCache() ; liveManager.getCacheIfNecessary() ; } public Object t1(){ return new Object() ; } }
@FunctionalInterface public interface LiveMapFetch<T> { // 异步刷新数据 T fetch(String key) ; } public class LiveMapManager<T> { private int cacheMillis; private Map<String,LiveCache<T>> liveCacheMap; private LiveMapFetch<T> liveMapFetch; private Logger logger = LoggerFactory.getLogger(LiveMapManager.class) ; private boolean refresh = false ; public LiveMapManager(int cacheMillis, LiveMapFetch<T> liveMapFetch) { this.cacheMillis = cacheMillis ; this.liveMapFetch = liveMapFetch ; } /** * fetch cache ; if cache expired , synchronous fetch * @return */ public T getCache(String key) { initLiveCache(); T t ; if(liveCacheMap.containsKey(key) && (t = liveCacheMap.get(key).getElement()) != null) { return t ; } else { t = liveMapFetch.fetch(key) ; if(t != null) { LiveCache<T> liveAccess = new LiveCache<T>(cacheMillis, t) ; liveCacheMap.put(key, liveAccess) ; return t ; } } return null ; } /** * fetch cache ; if cache expired , return old cache and asynchronous fetch * @return */ public T getCacheIfNecessary(String key) { initLiveCache(); T t ; if(liveCacheMap.containsKey(key) && (t = liveCacheMap.get(key).getElement()) != null) { return t ; } else { if(liveCacheMap.containsKey(key)) { refreshCache(key) ; return liveCacheMap.get(key).getElementIfNecessary() ; } else { t = liveMapFetch.fetch(key) ; if(t != null) { LiveCache<T> liveAccess = new LiveCache<T>(cacheMillis, t) ; liveCacheMap.put(key, liveAccess) ; return t ; } } } return t ; } /** * init liveCache */ private void initLiveCache() { if(liveCacheMap == null) { liveCacheMap = new HashMap<>() ; } } /** * asynchronous refresh cache */ private void refreshCache(String key) { if(refresh) return ; refresh = true ; try { Thread thread = new Thread(() -> { try { T t = liveMapFetch.fetch(key); if (t != null) { LiveCache<T> liveAccess = new LiveCache<>(cacheMillis, t); liveCacheMap.put(key, liveAccess); } } catch (Exception e) { logger.error("LiveMapManager.refreshCache thread error.key:",e); } finally { refresh = false ; } }) ; thread.start(); } catch (Exception e) { logger.error("LiveMapManager.refreshCache error.key:" + key, e); } } } public class Test { public static void main(String[] args) { int cacheMilis = 1000 ; LiveMapManager<Object> liveManager = new LiveMapManager<>(cacheMilis,(String key) -> new Test().t1(key)) ; liveManager.getCache("key") ; liveManager.getCacheIfNecessary("key") ; } public Object t1(String key){ return new Object() ; } }
标签:
原文地址:http://www.cnblogs.com/sten/p/5734512.html