Android開源架構Universal-Image-Loader學習無——WeakMemoryCache 和 FuzzyKeyMemoryCache

來源:互聯網
上載者:User

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();    }}


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.