阅读背景:

Android开源框架Universal-Image-Loader学习四——LimitedMemoryCache的一些子集

来源:互联网 

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);
    }
}/**
 * (cache size limit




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: