Java自定义LRU缓存实现
发布时间:2020-12-15 03:13:44 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 自定义LRU缓存实现 Cache缓存接口???? public interface Cache {/** * 缓存ID * @return */String getId(); Object get(String key);void put(String
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
自定义LRU缓存实现
Cache缓存接口????public interface Cache { /** * 缓存ID * @return */ String getId(); Object get(String key); void put(String key,Object value); void clear(); void remove(String key); int size(); } LRUCacheImpl????public class LRUCacheImpl implements Cache { private long timeout=0; //0代表永不超时 private Map<Object,CacheEntry> cache=null; private String id; public LRUCacheImpl(String id,int maxsize,long timeout){ this.timeout=timeout; this.cache=new LRULinkedHashMap(maxsize); this.id=id; } public LRUCacheImpl(int maxsize,long timeout){ this.timeout=timeout; this.cache=new LRULinkedHashMap(maxsize); this.id=UUID.randomUUID().toString(); } @Override public String getId() { // TODO Auto-generated method stub return this.id; } @Override public Object get(String key) { // TODO Auto-generated method stub Object value=null; CacheEntry entry; synchronized (this) { entry=cache.get(key); } if(entry!=null){ //如果没有超时,或者超时设置为0 if((System.currentTimeMillis()-entry.getTimeCached())<timeout||timeout==0){ value=entry.getValue(); }else{ cache.remove(key); } } return value; } @Override public synchronized void put(String key,Object value) { // TODO Auto-generated method stub cache.put(key,new DefaultCacheEntry(value,System.currentTimeMillis())); } @Override public synchronized void clear() { // TODO Auto-generated method stub cache.clear(); } @Override public synchronized void remove(String key) { // TODO Auto-generated method stub cache.remove(key); } @Override public int size() { // TODO Auto-generated method stub return cache.size(); } } [代码]LRULinkedHashMap ????public class LRULinkedHashMap extends LinkedHashMap<Object,CacheEntry>{ private int maxSize; public LRULinkedHashMap(int maxsize){ super(maxsize/2+1,0.75f,true); this.maxSize=maxsize; } /* * 当缓存数超过最大容量时,返回true把最后一个移除。 * @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry) */ @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size()>maxSize; } } [代码]CacheFactory ????public interface CacheFactory { Cache createCache(String id,long timeout); } [代码]LRUCacheFactory ????public class LRUCacheFactory implements CacheFactory{ @Override public Cache createCache(String id,long timeout) { // TODO Auto-generated method stub return new LRUCacheImpl(id,maxsize,timeout); } } [代码]CacheEntry????public interface CacheEntry { /** * 加入缓存时的时间 * @return */ long getTimeCached(); Object getValue(); } [代码]DefaultCacheEntry ????public class DefaultCacheEntry implements CacheEntry{ private long timecached=-1; private Object value; public DefaultCacheEntry(Object value,long timecached){ this.value=value; this.timecached=timecached; } @Override public long getTimeCached() { // TODO Auto-generated method stub return timecached; } @Override public Object getValue() { // TODO Auto-generated method stub return value; } } [代码]CacheManager????public class CacheManager{ private static final ConcurrentHashMap<String,Cache> caches=new ConcurrentHashMap<>(); /** * 缓存策略枚举 */ public static enum CacheStrategy{ LRU } /** * * @param maxsize * @param timeout * @param strategy 可为null。默认LRU * @return */ public static Cache createCache(int maxsize,long timeout,CacheStrategy strategy) { if(strategy==null){ strategy=CacheStrategy.LRU; } Cache cache=getCacheFactory(strategy).createCache(UUID.randomUUID().toString(),timeout); caches.putIfAbsent(cache.getId(),cache); return cache; } private static CacheFactory getCacheFactory(CacheStrategy strategy){ CacheFactory factory=null; if("LRU".equals(strategy.name())){ factory=new LRUCacheFactory(); } return factory; } public static Cache getCacheById(String cacheId){ return caches.get(cacheId); } public static void clear(){ caches.clear(); } public static void remove(String cacheId){ caches.remove(cacheId); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读