Android open-source framework Universal-Image-Loader learning 4 -- some subsets of LimitedMemoryCache
LRULimitedMemoryCache source code:
/*** (Cache size limited) Size of all stored bitmaps will not to exceed size limit. * (deletion policy 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. * use strong references within a specified value */public class LRULimitedMemoryCache extends LimitedMemoryCache {private static final int INITIAL_CAPACITY = 10; private static final float LOAD_FACTOR = 1.1f;/** LRU */private final Map
LruCache = Collections. synchronizedMap (new LinkedHashMap
(INITIAL_CAPACITY, LOAD_FACTOR, true);/** constructor */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
> It = lruCache. entrySet (). iterator (); if (it. hasNext () {Entry
Entry = it. next (); mostLongUsedValue = entry. getValue (); it. remove () ;}return mostLongUsedValue ;}@ Override protected Reference
CreateReference (Bitmap value) {return new WeakReference
(Value );}}
FIFOLimitedMemoryCache source code:
/*** Limited {@ link Bitmap bitmap} cache. provides {@ link Bitmap bitmaps} storing. size of all stored bitmaps will not to * exceed size limit. * deletion policy: 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 implements olimitedmemorycache extends LimitedMemoryCache {private final List
Queue = Collections. synchronizedList (new Functions List
(); Public parameter olimitedmemorycache (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/* Delete Policy: Delete the header node, that is, FIFO */protected Bitmap removeNext () {return queue. remove (0) ;}@ Override protected Reference
CreateReference (Bitmap value) {return new WeakReference
(Value );}}
LargestLimitedMemoryCache source code:
/*** Limited {@ link Bitmap bitmap} cache. provides {@ link Bitmap bitmaps} storing. size of all stored bitmaps will not to * exceed size limit. * deletion policy: Delete the largest 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
ValueSizes = Collections. synchronizedMap (new HashMap
(); 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/* deletion policy: traverse the map to find the maximum value */protected Bitmap removeNext () {Integer maxSize = null; Bitmap largestValue = null; Set
> Entries = valueSizes. entrySet (); synchronized (valueSizes) {for (Entry
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
CreateReference (Bitmap value) {return new WeakReference
(Value );}}