WeakMemoryCache 源码:
BaseMemoryCache参见:http://blog.csdn.net/woliuyunyicai/article/details/45330867
/**
* Memory cache with {@linkplain WeakReference weak references} to {@linkplain android.graphics.Bitmap bitmaps}<br />
* <br />
* <b>NOTE:</b> 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<Bitmap> createReference(Bitmap value) {
return new WeakReference<Bitmap>(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<String> keyComparator;
public FuzzyKeyMemoryCache(MemoryCache cache, Comparator<String> 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<String> keys() {
return cache.keys();
}
}