中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android中的緩存

2018-07-20    來(lái)源:編程學(xué)習(xí)網(wǎng)

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

為什么會(huì)用到緩存呢?主要是流量耗不起啊,國(guó)內(nèi)的公共場(chǎng)所的WiFi的普及率不高,因此必須考慮流量的問(wèn)題,說(shuō)白了,就是用戶體驗(yàn)啊,每次都網(wǎng)絡(luò)請(qǐng)求,消耗資源不說(shuō),網(wǎng)速不好的情況下還會(huì)有網(wǎng)絡(luò)延時(shí),用戶體驗(yàn)不好。

Android中的緩存,從方式上來(lái)說(shuō),一般有網(wǎng)絡(luò)緩存,磁盤(pán)緩存即SD卡緩存,內(nèi)存緩存。網(wǎng)絡(luò)緩存需要服務(wù)端的配合,用于加快網(wǎng)絡(luò)請(qǐng)求的響應(yīng)速度。磁盤(pán)緩存一般用DiskLruCache,當(dāng)然也可以用SqlLite數(shù)據(jù)庫(kù),以及sharedpreference等作持久化處理。這里主要說(shuō)下兩種常用的緩存方法,LruCache、DiskLruCache。前者用于內(nèi)存緩存,后者用于設(shè)備緩存,一般兩者結(jié)合起來(lái)效果更好。

其實(shí)緩存的實(shí)現(xiàn)并不難,每一中緩存都會(huì)有三個(gè)基本操作,添加、獲取、刪除。了解這些了,就會(huì)有思路了。

再說(shuō)LruCache、DiskLruCache,可以看到,兩者都有Lru,那么Lru是什么呢?這是目前常用的一種緩存算法:近期最少使用算法,核心思想很簡(jiǎn)單,就是當(dāng)緩存滿時(shí),會(huì)優(yōu)先刪除那些近期最少使用的緩存。那么現(xiàn)在分別了解下這兩種緩存吧。

LruCache

LruCache內(nèi)部用到的是LinkedHashMap,LinkedHashMap與HashMap的不同住處在于LinkedHashMap 維護(hù)著一個(gè)運(yùn)行于所有條目的雙重鏈接列表。此鏈接列表定義了迭代順序,該迭代順序可以是插入順序或者是訪問(wèn)順序。也就說(shuō)它的插入和訪問(wèn)是有順序的。另外LruCache是線程安全的。至于使用的話就很簡(jiǎn)單了。

// 初始化 int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight() / 1024;
        }
    };

總緩存大小一般會(huì)設(shè)置為當(dāng)前進(jìn)程可用內(nèi)存的1/8,當(dāng)然這個(gè)數(shù)是可以自己設(shè)置的,這個(gè)數(shù)是推薦的。sizeOf方法是為了計(jì)算緩存對(duì)象的大小。如果有必要也可以重寫(xiě)entryRemoved來(lái)完成某些資源回收工作。

再看緩存的添加與刪除,

//添加緩存 mMemoryCache.put(key,bitmap); //獲取緩存 mMemoryCache.get(key); //刪除緩存 mMemoryCache.remove(key);

DiskLruCache

DiskLruCache用與磁盤(pán)緩存,被官方推薦使用。下面來(lái)看看它的使用。

自從用了Gradle后,引入項(xiàng)目方便多了,誰(shuí)用誰(shuí)知道。

compile 'com.jakewharton:disklrucache:2.0.2'

創(chuàng)建DiskLruCache:

DiskLruCache mDiskLruCache = null; try {  
    File cacheDir = getDiskCacheDir(context, "bitmap"); if (!cacheDir.exists()) {  
        cacheDir.mkdirs();  
    }  
    mDiskLruCache = DiskLruCache.open(cacheDir, 1, 1, 10 * 1024 * 1024);  
} catch (IOException e) {  
    e.printStackTrace();  
}

解釋下DiskLruCache.open的參數(shù),第一個(gè)表示存儲(chǔ)的路徑,第二個(gè)表示應(yīng)用的版本號(hào),注意這里當(dāng)版本號(hào)發(fā)生改變時(shí)會(huì)清空之前所有的緩存文件,而在實(shí)際開(kāi)發(fā)中這個(gè)性質(zhì)用的不多,所以直接寫(xiě)1。第三個(gè)表示單個(gè)節(jié)點(diǎn)對(duì)應(yīng)的數(shù)據(jù)的個(gè)數(shù),設(shè)置為1就可以了,第四個(gè)表示緩存的總大小,當(dāng)超出這個(gè)值時(shí),會(huì)清除一些緩存保證總大小不大于這個(gè)設(shè)定的值。

添加緩存:

第一步,網(wǎng)絡(luò)下載圖片(文件也是一樣的步驟的)并通過(guò)outputStream寫(xiě)入到本地

private boolean downloadUrlToStream(String urlString, OutputStream outputStream) {  
HttpURLConnection urlConnection = null;  
BufferedOutputStream out = null;  
BufferedInputStream in = null; try { final URL url = new URL(urlString);  
    urlConnection = (HttpURLConnection) url.openConnection();  
    in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);  
    out = new BufferedOutputStream(outputStream, 8 * 1024); int b; while ((b = in.read()) != -1) {  
        out.write(b);  
    } return true;  
} catch (final IOException e) {  
    e.printStackTrace();  
} finally { if (urlConnection != null) {  
        urlConnection.disconnect();  
    } try { if (out != null) {  
            out.close();  
        } if (in != null) {  
            in.close();  
        }  
    } catch (final IOException e) {  
        e.printStackTrace();  
    }  
} return false;  
}

第二步,處理緩存的key,直接用url作為key值時(shí)最有快捷的方式,但是url里會(huì)有特殊字符,不符合Android的命名規(guī)范,最好的辦法就是把url進(jìn)行MD5摘要。

public String hashKeyForDisk(String key) {  
    String cacheKey; try { final MessageDigest mDigest = MessageDigest.getInstance("MD5");  
        mDigest.update(key.getBytes());  
        cacheKey = bytesToHexString(mDigest.digest());  
    } catch (NoSuchAlgorithmException e) {  
        cacheKey = String.valueOf(key.hashCode());  
    } return cacheKey;  
} private String bytesToHexString(byte[] bytes) {  
    StringBuilder sb = new StringBuilder(); for (int i = 0; i < bytes.length; i++) {  
        String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) {  
            sb.append('0');  
        }  
        sb.append(hex);  
    } return sb.toString();  
}

第三步 創(chuàng)建DiskLruCache.Editor的實(shí)例,寫(xiě)入數(shù)據(jù)

String key = hashKeyForDisk(imageUrl);  
            DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) {  
                OutputStream outputStream = editor.newOutputStream(0); if (downloadUrlToStream(imageUrl, outputStream)) {  
                    editor.commit();  
                } else {  
                    editor.abort();  
                }  
            }  
            mDiskLruCache.flush();

editor.commit()方法用來(lái)提交寫(xiě)入操作,editor.abort()回退整個(gè)操作。

讀取緩存:

Bitmap bitmap = null;
        String key = hashKeyFormUrl(url);
        DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key); if (snapShot != null) {
            FileInputStream fileInputStream = (FileInputStream)snapShot.getInputStream(0);
            FileDescriptor fileDescriptor = fileInputStream.getFD();
            bitmap = mImageResizer.decodeSampledBitmapFromFileDescriptor(fileDescriptor,
                    reqWidth, reqHeight); if (bitmap != null) {
                addBitmapToMemoryCache(key, bitmap);
            }
        }
public Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd, null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFileDescriptor(fd, null, options);
    }

需要說(shuō)明下的是為了避免加載圖片時(shí)導(dǎo)致OOM,不建議直接加在Bitmap,通常我們會(huì)通過(guò)BitmapFactory.Options來(lái)加載一張縮放的圖片,但是這中方法對(duì)于FileInputStream有問(wèn)題,因?yàn)镕ileInputStream是有序的文件流,而兩次的從的 decodeStream調(diào)用影響了文件流的位置屬性,導(dǎo)致第二次decodeStream時(shí)得到的為null。為了解決這個(gè)問(wèn)題,可以先得到對(duì)應(yīng)的文件描述符,然后通過(guò)BitmapFactory.decodeFileDescriptor()來(lái)加載圖片。

移除緩存:

mDiskLruCache.remove(key);

 

來(lái)自:http://www.jianshu.com/p/96a7865fdab4

標(biāo)簽: 安全 數(shù)據(jù)庫(kù) 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Swift3新特性

下一篇:App啟動(dòng)優(yōu)化-Android性能優(yōu)化