LRULimitedMemoryCache源码:
/** * (cache size limited)Size of all stored bitmaps will not to exceed size limit. * (删除策略LRU)When cache reaches limit size then the least recently used bitmap is deleted from cache. * NOTE: This cache uses strong and weak references for stored Bitmaps. * 限定值内的使用强引用 */ public class LRULimitedMemoryCache extends LimitedMemoryCache { private static final int INITIAL_CAPACITY = 10; private static final float LOAD_FACTOR = 1.1f; /** LRU */ private final Map<String, Bitmap> lruCache = Collections.synchronizedMap(new LinkedHashMap<String, Bitmap>(INITIAL_CAPACITY, LOAD_FACTOR, true)); /**构造函数 */ public LRULimitedMemoryCache(int maxSize) { super(maxSize); } @Override public boolean put(String key, Bitmap value) { if (super.put(key, value)) { lruCache.put(key, value); return true; } else { return false; } } @Override public Bitmap get(String key) { lruCache.get(key); // call "get" for LRU logic return super.get(key); } @Override public Bitmap remove(String key) { lruCache.remove(key); return super.remove(key); } @Override public void clear() { lruCache.clear(); super.clear(); } @Override protected int getSize(Bitmap value) { return value.getRowBytes() * value.getHeight(); } @Override protected Bitmap removeNext() { Bitmap mostLongUsedValue = null; synchronized (lruCache) { Iterator<Entry<String, Bitmap>> it = lruCache.entrySet().iterator(); if (it.hasNext()) { Entry<String, Bitmap> entry = it.next(); mostLongUsedValue = entry.getValue(); it.remove(); } } return mostLongUsedValue; } @Override protected Reference<Bitmap> createReference(Bitmap value) { return new WeakReference<Bitmap>(value); } }
FIFOLimitedMemoryCache源码:
/** * Limited {@link Bitmap bitmap} cache. Provides {@link Bitmap bitmaps} storing. Size of all stored bitmaps will not to * exceed size limit. * 删除策略:FIFO * NOTE:This cache uses strong and weak references for stored Bitmaps. Strong references - for limited count of * Bitmaps (depends on cache size), weak references - for all other cached Bitmaps. */ public class FIFOLimitedMemoryCache extends LimitedMemoryCache { private final List<Bitmap> queue = Collections.synchronizedList(new LinkedList<Bitmap>()); public FIFOLimitedMemoryCache(int sizeLimit) { super(sizeLimit); } @Override public boolean put(String key, Bitmap value) { if (super.put(key, value)) { queue.add(value); return true; } else { return false; } } @Override public Bitmap remove(String key) { Bitmap value = super.get(key); if (value != null) { queue.remove(value); } return super.remove(key); } @Override public void clear() { queue.clear(); super.clear(); } @Override protected int getSize(Bitmap value) { return value.getRowBytes() * value.getHeight(); } @Override /*删除策略:删除头结点,即FIFO*/ protected Bitmap removeNext() { return queue.remove(0); } @Override protected Reference<Bitmap> createReference(Bitmap value) { return new WeakReference<Bitmap>(value); } }
LargestLimitedMemoryCache源码:
/** * Limited {@link Bitmap bitmap} cache. Provides {@link Bitmap bitmaps} storing. Size of all stored bitmaps will not to * exceed size limit. * 删除策略: 删除size最大的bitmap * NOTE:This cache uses strong and weak references for stored Bitmaps. Strong references - for limited count of * Bitmaps (depends on cache size), weak references - for all other cached Bitmaps */ public class LargestLimitedMemoryCache extends LimitedMemoryCache { private final Map<Bitmap, Integer> valueSizes = Collections.synchronizedMap(new HashMap<Bitmap, Integer>()); public LargestLimitedMemoryCache(int sizeLimit) { super(sizeLimit); } @Override public boolean put(String key, Bitmap value) { if (super.put(key, value)) { valueSizes.put(value, getSize(value)); return true; } else { return false; } } @Override public Bitmap remove(String key) { Bitmap value = super.get(key); if (value != null) { valueSizes.remove(value); } return super.remove(key); } @Override public void clear() { valueSizes.clear(); super.clear(); } @Override protected int getSize(Bitmap value) { return value.getRowBytes() * value.getHeight(); } @Override /*删除策略:遍历map查找最大值*/ protected Bitmap removeNext() { Integer maxSize = null; Bitmap largestValue = null; Set<Entry<Bitmap, Integer>> entries = valueSizes.entrySet(); synchronized (valueSizes) { for (Entry<Bitmap, Integer> entry : entries) { if (largestValue == null) { largestValue = entry.getKey(); maxSize = entry.getValue(); } else { Integer size = entry.getValue(); if (size > maxSize) { maxSize = size; largestValue = entry.getKey(); } } } } valueSizes.remove(largestValue); return largestValue; } @Override protected Reference<Bitmap> createReference(Bitmap value) { return new WeakReference<Bitmap>(value); } }
时间: 2024-12-05 10:55:20