Android開源架構Universal-Image-Loader學習無——WeakMemoryCache 和 FuzzyKeyMemoryCache
/** * Memory cache with {@linkplain WeakReference weak references} to {@linkplain android.graphics.Bitmap bitmaps} * * NOTE: This cache uses only weak references for stored Bitmaps. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @since 1.5.3 */public class WeakMemoryCache extends BaseMemoryCache { @Override protected Reference createReference(Bitmap value) { return new WeakReference(value); }}FuzzyKeyMemoryCache源碼:
/** * MemoryCache的裝飾者模式。為cache提供一個特殊功能:(使用Comparator)使得一些不同的keys被當做是等價的。當使用key put一些值到cache中 * 具有“相同”意義的keys將會先被移除(一般不會用到該class) * NOTE: Used for internal needs. Normally you don't need to use this class. */public class FuzzyKeyMemoryCache implements MemoryCache { private final MemoryCache cache; private final Comparator keyComparator; public FuzzyKeyMemoryCache(MemoryCache cache, Comparator keyComparator) { this.cache = cache; this.keyComparator = keyComparator; } @Override public boolean put(String key, Bitmap value) { // Search equal key and remove this entry synchronized (cache) { String keyToRemove = null; for (String cacheKey : cache.keys()) { if (keyComparator.compare(key, cacheKey) == 0) { keyToRemove = cacheKey; break; } } if (keyToRemove != null) { cache.remove(keyToRemove); } } return cache.put(key, value); } @Override public Bitmap get(String key) { return cache.get(key); } @Override public Bitmap remove(String key) { return cache.remove(key); } @Override public void clear() { cache.clear(); } @Override public Collection keys() { return cache.keys(); }}