一、前言
对于abdera-client包LRUCache内存管理的设计(LRU是Least Recently Used的缩写,即最近最久未使用,常用于页面置换算法,是为虚拟页式存储管理服务),打开org.apache.abdera.protocol.client.cache.LRUMap源码,可以看到其LRU实现基于java.util.LinkedHashMap。
二、源码说明
1.LRUMap -自定义LRU的Map代码
package org.apache.abdera.protocol.client.cache;@b@@b@import java.util.LinkedHashMap;@b@import java.util.Map.Entry;@b@@b@public final class LRUMap<A, B> extends LinkedHashMap<A, B>@b@{@b@ private static final long serialVersionUID = -8243948270889739367L;@b@ private final int size;@b@@b@ public LRUMap(int initialSize, float loadFactor)@b@ {@b@ super(initialSize, loadFactor, true);@b@ this.size = initialSize;@b@ }@b@@b@ protected boolean removeEldestEntry(Map.Entry<A, B> eldest) {@b@ return (size() > this.size);@b@ }@b@}
2.定义缓存接口Cache
package org.apache.abdera.protocol.client.cache;@b@@b@import org.apache.abdera.protocol.client.ClientResponse;@b@import org.apache.abdera.protocol.client.RequestOptions;@b@@b@public abstract interface Cache extends Iterable<Object>@b@{@b@ public abstract Disposition disposition(Object paramObject, RequestOptions paramRequestOptions);@b@@b@ public abstract CachedResponse get(Object paramObject);@b@@b@ public abstract Cache clear();@b@@b@ public abstract Cache remove(Object paramObject);@b@@b@ public abstract ClientResponse update(Object paramObject, RequestOptions paramRequestOptions, ClientResponse paramClientResponse1, ClientResponse paramClientResponse2);@b@@b@ public static enum Disposition@b@ {@b@ STALE, FRESH, TRANSPARENT;@b@ }@b@}
3.LRUCache缓存接口实现类
package org.apache.abdera.protocol.client.cache;@b@@b@import org.apache.abdera.Abdera;@b@@b@public class LRUCache extends InMemoryCache@b@ implements Cache@b@{@b@ private static final int DEFAULT_SIZE = 10;@b@@b@ public LRUCache(Abdera abdera)@b@ {@b@ this(abdera, 10);@b@ }@b@@b@ public LRUCache(Abdera abdera, int size) {@b@ super(abdera, new LRUMap(size, 0.75F));@b@ }@b@}
4.CacheFactory缓存工厂类接口
package org.apache.abdera.protocol.client.cache;@b@@b@import org.apache.abdera.Abdera;@b@@b@/**@b@ * @deprecated@b@ */@b@public abstract interface CacheFactory@b@{@b@ public abstract Cache getCache(Abdera paramAbdera);@b@}
5.LRUCacheFactory工厂实用类
package org.apache.abdera.protocol.client.cache;@b@@b@import org.apache.abdera.Abdera;@b@@b@/**@b@ * @deprecated@b@ */@b@public class LRUCacheFactory@b@ implements CacheFactory@b@{@b@ public LRUCacheFactory(Abdera abdera)@b@ {@b@ }@b@@b@ public LRUCacheFactory()@b@ {@b@ }@b@@b@ public Cache getCache(Abdera abdera)@b@ {@b@ return new LRUCache(abdera);@b@ }@b@}
三、相关源码
对于active-mq的org.activemq.util.LRUCache源码如下
package org.activemq.util;@b@@b@import java.util.LinkedHashMap;@b@import java.util.Map.Entry;@b@@b@public class LRUCache extends LinkedHashMap@b@{@b@ private static final long serialVersionUID = -5754338187296859149L;@b@ protected static final int DEFAULT_INITIAL_CAPACITY = 1000;@b@ protected static final float DEFAULT_LOAD_FACTOR = 0.75F;@b@ private int maxSize;@b@@b@ public LRUCache(int initialCapacity, float loadFactor, boolean accessOrder, int maxSize)@b@ {@b@ super(initialCapacity, loadFactor, accessOrder);@b@ this.maxSize = maxSize;@b@ }@b@@b@ public LRUCache(int maxSize) {@b@ this(1000, 0.75F, true, maxSize);@b@ }@b@@b@ public LRUCache(int maxSize, boolean accessOrder) {@b@ this(1000, 0.75F, accessOrder, maxSize);@b@ }@b@@b@ protected boolean removeEldestEntry(Map.Entry eldest) {@b@ return (size() > this.maxSize);@b@ }@b@}