LruCache機制

一、Lrucache簡介

1. 什么是LruCache

LRU的全稱是Least Recently Used,即最近最少使用,LruCache 的實現(xiàn)原理就是把近期最少使用的數(shù)據(jù)從緩存中移除,保留使用最頻繁的數(shù)據(jù)。LruCache內(nèi)部采用的是LinkedHashMap,LruCache 作為內(nèi)存緩存,使用強引用方式緩存有限個數(shù)據(jù),當(dāng)緩存的某個數(shù)據(jù)被訪問時,它就會被移動到隊列的頭部,當(dāng)一個新數(shù)據(jù)要添加到LruCache而此時緩存大小要滿時,隊尾的數(shù)據(jù)就有可能會被垃圾回收器(GC)回收掉。
下面是LruCache類源碼文檔:

A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.

2. LruCache常用方法

void resize(int maxSize)    //更新存儲大小
V put(K key, V value)   //存數(shù)據(jù),返回之前key對應(yīng)的value,如果沒有,返回null
V get(K key)    //取出key對應(yīng)的緩存數(shù)據(jù)
V remove(K key) //移除key對應(yīng)的value
void evictAll() //清空緩存數(shù)據(jù)
Map<K, V> snapshot()    //復(fù)制一份緩存并返回,順序從最近最少訪問到最多訪問排序

二、LruCache的簡單使用

下面通過一個下載網(wǎng)絡(luò)圖片進行緩存到本地的例子進行學(xué)習(xí),在第一次加載的時候是建立網(wǎng)絡(luò)連接進行下載,在下載之后再進行圖片顯示的時候利用已經(jīng)緩存的數(shù)據(jù)進行顯示。LruCache是內(nèi)存緩存,緩存的大小可以自己設(shè)置,通常設(shè)置為手機內(nèi)存的1/8。下面進行使用的操作:

1. 新建一個圖片加載類,用來對緩存進行操作,實例化LruCache類,并重寫sizeof方法

public class LruCacheTest {

    private LruCache<String,Bitmap> mLruCache;

    public void initLruCache(){
        long maxMemory = Runtime.getRuntime ().maxMemory ();//手機的最大內(nèi)存
        int cacheSize = (int) maxMemory/8;//設(shè)置緩存的大小
        mLruCache = new LruCache<String, Bitmap> (cacheSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount ();
            }
        };
    }   
}

2. 創(chuàng)建下載圖片,緩存圖片的方法

 // 把Bitmap對象加入到緩存中
    public void saveBitmapToCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            lruCache.put(key, bitmap);
        }
    }

    // 從緩存中得到Bitmap對象
    public Bitmap getBitmapFromMemCache(String key) {
    Log.i(TAG, "lrucache size: " + lruCache.size());
        return lruCache.get(key);
    }

    // 從緩存中刪除指定的Bitmap
    public void removeBitmapFromMemory(String key) {
        lruCache.remove(key);
    }  

3. 新建一個內(nèi)部任務(wù)類進行線程池操作,在之前線程池的代碼上進行改進

public class ThreadPoolUtils {
    /**
     * 線程池大小
     */
    private static final int CORE_POOL_SIZE = 4;

    /**
     * 最大線程數(shù)
     */
    private static final int MAXIMUM_POOL_SIZE = 5;

    /**
     * 空閑線程存活時間為2秒
     */
    private static final long KEEP_ALIVE_TIME = 2;

    private static ThreadPoolUtils threadPoolUtils;
    private ThreadPoolExecutor mThreadPoolExecutor;
    private BlockingQueue<Runnable> mBlockingDeque;
    private Context mContext;
    private Handler handler;
    public LruCache<String, Bitmap> imageLruCache;

    private ThreadPoolUtils() {
    }

    public static ThreadPoolUtils getInstance() {
        if(threadPoolUtils == null) {
            synchronized (ThreadPoolUtils.class) {
                if(threadPoolUtils == null) {
                    threadPoolUtils = new ThreadPoolUtils ();
                }
            }
        }
        return threadPoolUtils;
    }

    public void initThreadPool(Context context) {
        this.mContext = context.getApplicationContext ();
        mBlockingDeque = new LinkedBlockingDeque<> ();
        handler = new Handler ();
        mThreadPoolExecutor = new ThreadPoolExecutor (CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, mBlockingDeque);
        long maxMemory = Runtime.getRuntime ().maxMemory ();
        int cacheSize = (int) (maxMemory / 8);
        imageLruCache = new LruCache<String, Bitmap> (cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getHeight () / 1024;
            }
        };
    }

    public void startThread(String imageUrl, ImageView iv) {
        try {
            ThreadPoolTask task = new ThreadPoolTask (imageUrl, iv);
            mThreadPoolExecutor.execute (task);
        } catch (Exception e) {
            Log.e ("threadtest", "AbortPolicy...已超出規(guī)定的線程數(shù)量,不能再增加了....");
        }
    }

    public void saveBitmapToCache(String key, Bitmap bitmap) {
        if(getBitmapFromCache (key) == null) {
            imageLruCache.put (key, bitmap);
            Log.i ("cache size", "is " + imageLruCache.size ());
        }
    }

    public Bitmap getBitmapFromCache(String key) {
        return imageLruCache.get (key);
    }

    public void removeCache(String key) {
        imageLruCache.remove (key);
    }

    public class ThreadPoolTask implements Runnable {

        private String imageUrl;
        private ImageView imageView;
        private Bitmap bitmap;

        public ThreadPoolTask(String url, ImageView iv) {
            this.imageUrl = url;
            this.imageView = iv;
        }

        @Override
        public void run() {
            boolean flag = true;
            try {
                while (flag) {
                    bitmap = downImage (imageUrl);
                    threadPoolUtils.saveBitmapToCache ("IdImage", bitmap);
                    Log.i ("CacheSize", "is" + threadPoolUtils.getBitmapFromCache ("IdImage").getByteCount ());
                    //圖片下載完成,handler通知主線程更新界面
                    if(handler != null) {
                        handler.post (new Runnable () {
                            @Override
                            public void run() {
                                imageView.setImageBitmap (bitmap);
                                MainActivity.lruCache = imageLruCache;
                                Toast.makeText (mContext, "Bitmap size is " + bitmap.getHeight (), Toast.LENGTH_SHORT).show ();
                            }
                        });
                    }
                    flag = false;
                }
            } catch (Exception e) {
                e.printStackTrace ();
            }
        }

        public Bitmap downImage(String imageUrl) {
            InputStream inputStream = null;
            Bitmap bitmap = null;
            URL url;
            HttpURLConnection connection = null;
            try {
                url = new URL (imageUrl);
                connection = (HttpURLConnection) url.openConnection ();
                connection.connect ();
                inputStream = connection.getInputStream ();
                bitmap = BitmapFactory.decodeStream (inputStream);
                inputStream.close ();
            } catch (IOException e) {
                e.printStackTrace ();
            } finally {
                if(connection != null) {
                    connection.disconnect ();
                }
                if(inputStream != null) {
                    try {
                        inputStream.close ();
                    } catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
            }
            return bitmap;
        }
    }
}

4. 在需要進行圖片顯示的地方進行圖片下載和加載,第二次加載緩存里面的內(nèi)容

//首先判斷緩存里面是否有圖片
final Bitmap bitmap = MainActivity.lruCache.get ("IdImage");
        if(bitmap == null) {
            String imgUrl = "https://upload-images.jianshu.io/upload_images/13206622-5c1797f186484061.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/460";
            threadPoolUtils.startThread (imgUrl, ivProfilePhoto);
        } else {
            ivProfilePhoto.setImageBitmap (bitmap);
            Toast.makeText (getActivity (), "cache size is " + bitmap.getHeight (), Toast.LENGTH_SHORT).show ();
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,236評論 3 119
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評論 25 708
  • 一個沒有創(chuàng)作力的國家怎能傲立于世界之巔,寫給全體藝術(shù)創(chuàng)作者,請原諒?fù)磔叺妮p狂。 有一個很痛心的事實,那就是內(nèi)地迄今...
    懷俠閱讀 370評論 4 2
  • 說實話,你已經(jīng)15歲這件事情著實讓我吃了一驚。因為我還沒有過16歲生日,所以老妹相當(dāng)于和我一個年紀了。不過,我的1...
    小小白陽閱讀 453評論 0 3
  • 今天一個人在家哭著看完了釜山行,我想,說這部電影不好看的應(yīng)該是都沒認真看的吧,里面很多關(guān)于人性抉擇的時候其實都可以...
    mochascafe閱讀 602評論 0 47

友情鏈接更多精彩內(nèi)容