Volley框架解析(六)-----Cache接口及其默認實現類解析

Volley框架解析(六)—–Cache接口及其默認實現類解析


1. 前言(可直接無視跳過

    不知不覺Volley的源碼分析到了Cache接口部分了,前面涉及到網絡的部分都介紹完了,在處理網絡請求返回的數據時,會根據request結果是否需要緩存來進行不同的處理。如需要緩存結果,就涉及到了Cache.java及其默認實現類DiskBasedCache.java。 其實在之前什麼都不知道的時候,認爲緩存是個非常神祕的東西,可能是人類對於未知的恐懼和敬畏嘛orz,後來在用到另外一個牛掰的網絡請求框架ion的時候,在自己app的目錄下面看到一個ion的文件夾= =,進去之後發現了好多balabala亂七八糟的打不開的文件,突然明白了,估計是看到本地緩存的文件了。扯遠了= =,開始源代碼的分析。

2. Cache.java

    用於處理緩存的接口,裏面有很多抽象的方法等着被實現,裏面還有一個static類Entry,裏面有些關於緩存的單元信息。(例如,緩存的內容,緩存過期的時間,緩存需要刷新的時間等等)。

package com.android.volley;

/**
 * An interface for a cache keyed by a String with * a byte array as data.
 * 一個用於緩存的接口
 */
public interface Cache {
    /**
     * Retrieves an entry from the cache.
     * 用來獲取緩存的入口,通過傳入的key
     * 
     * @param key Cache key
     * 這個key應該是request對應其緩存的唯一key
     *
     * @return An {@link Entry} or null in the event of a cache miss
     */
    public Entry get(String key);

    /**
     * Adds or replaces an entry to the cache.
     * 添加或者給request更新緩存
     * @param key Cache key
     * @param entry Data to store and metadata for cache coherency, TTL, etc.
     */
    public void put(String key, Entry entry);

    /**
     * Performs any potentially long-running actions needed to initialize the cache;
     * will be called from a worker thread.
     */
    public void initialize();

    /**
     * Invalidates an entry in the cache.
     * 這個函數是將key對應的緩存置於過期
     * 分爲fully expire和soft expire, 目前還不知道是什麼意思 = =
     * 
     * @param key Cache key
     * @param fullExpire True to fully expire the entry, false to soft expire
     */
    public void invalidate(String key, boolean fullExpire);

    /**
     * Removes an entry from the cache.
     * 將key對應的緩存直接移除掉
     * 
     * @param key Cache key
     */
    public void remove(String key);

    /**
     * Empties the cache.
     * 清除所有的緩存
     */
    public void clear();

    /**
     * Data and metadata for an entry returned by the cache.
     */
    public static class Entry {
        /** The data returned from cache. */
        public byte[] data;

        /** ETag for cache coherency. */
        public String etag;

        /** Date of this response as reported by the server. */
        public long serverDate;

        /** The last modified date for the requested object. */
        public long lastModified;

        /** TTL for this record.
         *  根據後面的isExpired()函數來看
         *  該條數據的意思應該是緩存過期的時間 
         */
        public long ttl;

        /** Soft TTL for this record. 
         *  根據refreshNeeded()函數來看
         *  意思是需要更新緩存的時間點
         */
        public long softTtl;

        /** Immutable response headers as received from server; must be non-null. */
        public Map<String, String> responseHeaders = Collections.emptyMap();

       /** 
        * True if the entry is expired. 
        * 用來查看緩存是否過期了
        */
        public boolean isExpired() {
            return this.ttl < System.currentTimeMillis();
        }

        /** True if a refresh is needed from the original data source. */
        public boolean refreshNeeded() {
            return this.softTtl < System.currentTimeMillis();
        }
    }

}

3. DiskBasedCache.java

    實現了Cache.java接口,專門和本地存儲的文件打交道,負責緩存的寫入與讀取。涉及到了一部分InputStream和OutputStream還有File的知識。

/**
 * Cache implementation that caches files directly onto the hard disk in the specified
 * directory. The default disk usage size is 5MB, but is configurable.
 * 實現了Cache接口
 * 專門用於和本地文件交互的一個類
 * 存入緩存和取出緩存等功能
 */
public class DiskBasedCache implements Cache {

    /** 
     * Map of the Key, CacheHeader pairs 
     * CacheHeader.java爲本類中的一個static類
     * 裏面存放着一些
     */
    private final Map<String, CacheHeader> mEntries =
            new LinkedHashMap<String, CacheHeader>(16, .75f, true);

    /** 
     * Total amount of space currently used by the cache in bytes. 
     * 當前緩存的總大小
     */
    private long mTotalSize = 0;

    /** 
     * The root directory to use for the cache. 
     * 緩存的根目錄
     */
    private final File mRootDirectory;

    /** 
     * The maximum size of the cache in bytes. 
     * 緩存能接受的最大字節數
     */
    private final int mMaxCacheSizeInBytes;

    /** 
     * Default maximum disk usage in bytes.
     * 默認緩存能使用的最大空間
     */
    private static final int DEFAULT_DISK_USAGE_BYTES = 5 * 1024 * 1024;

    /** 
     * High water mark percentage for the cache
     * 類似於水位警戒線一樣的標識
     */
    private static final float HYSTERESIS_FACTOR = 0.9f;

    /** 
     * Magic number for current version of cache file format. 
     * 
     */
    private static final int CACHE_MAGIC = 0x20150306;

    /**
     * Constructs an instance of the DiskBasedCache at the specified directory.
     * 在指定的目錄下面創建一個DiskBasedCache
     *
     * @param rootDirectory The root directory of the cache.
     * @param maxCacheSizeInBytes The maximum size of the cache in bytes.
     */
    public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) {
        mRootDirectory = rootDirectory;
        mMaxCacheSizeInBytes = maxCacheSizeInBytes;
    }

    /**
     * Constructs an instance of the DiskBasedCache at the specified directory using
     * the default maximum cache size of 5MB.
     * @param rootDirectory The root directory of the cache.
     */
    public DiskBasedCache(File rootDirectory) {
        this(rootDirectory, DEFAULT_DISK_USAGE_BYTES);
    }

    /**
     * Clears the cache. Deletes all cached files from disk.
     * 清除當前目錄下的緩存,刪除所有緩存文件 
     */
    @Override
    public synchronized void clear() {
        File[] files = mRootDirectory.listFiles();
        if (files != null) {
            for (File file : files) {
                file.delete();
            }
        }
        mEntries.clear();
        mTotalSize = 0;
        VolleyLog.d("Cache cleared.");
    }

    /**
     * Returns the cache entry with the specified key if it exists, null otherwise.
     * 通過特殊的key,來獲取與緩存交流的接口(entry)
     * 如果沒有的話則返回null
     */
    @Override
    public synchronized Entry get(String key) {

        CacheHeader entry = mEntries.get(key);
        // if the entry does not exist, return.
        if (entry == null) {
            return null;
        }

        //依據key獲取緩存的文件,如果不存在則創建一個
        File file = getFileForKey(key);

        CountingInputStream cis = null;

        try {

            cis = new CountingInputStream(new BufferedInputStream(new FileInputStream(file)));
            CacheHeader.readHeader(cis); // eat header
            byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
            return entry.toCacheEntry(data);

        } catch (IOException e) {
            VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
            remove(key);
            return null;
        }  catch (NegativeArraySizeException e) {
            VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
            remove(key);
            return null;
        } finally {
            if (cis != null) {
                try {
                    cis.close();
                } catch (IOException ioe) {
                    return null;
                }
            }
        }
    }

    /**
     * Initializes the DiskBasedCache by scanning for all files currently in the
     * specified root directory. Creates the root directory if necessary.
     * 對緩存目錄的初始化工作,檢查目錄是否存在
     * 如果不存在就給重新創建一個
     */
    @Override
    public synchronized void initialize() {
        if (!mRootDirectory.exists()) {
            if (!mRootDirectory.mkdirs()) {
                VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
            }
            return;
        }

        /**
         * 如果緩存目錄已經存在了
         * 則將緩存目錄下面的文件都掃描一遍
         * 將關於緩存文件的部分信息加載到內存中來
         * 方便後面對緩存的查詢等工作
         */

        File[] files = mRootDirectory.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            BufferedInputStream fis = null;
            try {
                fis = new BufferedInputStream(new FileInputStream(file));
                CacheHeader entry = CacheHeader.readHeader(fis);
                entry.size = file.length();
                putEntry(entry.key, entry);
            } catch (IOException e) {
                if (file != null) {
                   file.delete();
                }
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException ignored) { }
            }
        }
    }

    /**
     * Invalidates an entry in the cache.
     * 將key對應的緩存作廢
     * 如果fullExpire爲true,則將整個entry作廢
     * 如果爲false,則只是軟作廢,也就是將緩存置於需要刷新的狀態
     *
     * @param key Cache key
     * @param fullExpire True to fully expire the entry, false to soft expire
     */
    @Override
    public synchronized void invalidate(String key, boolean fullExpire) {
        Entry entry = get(key);
        if (entry != null) {
            entry.softTtl = 0;
            if (fullExpire) {
                entry.ttl = 0;
            }
            put(key, entry);
        }

    }

    /**
     * Puts the entry with the specified key into the cache.
     * 將entry中包含的信息存放到key對應的緩存文件中去
     */
    @Override
    public synchronized void put(String key, Entry entry) {

        pruneIfNeeded(entry.data.length);

        File file = getFileForKey(key);
        try {
            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
            CacheHeader e = new CacheHeader(key, entry);
            boolean success = e.writeHeader(fos);
            if (!success) {
                fos.close();
                VolleyLog.d("Failed to write header for %s", file.getAbsolutePath());
                throw new IOException();
            }
            fos.write(entry.data);
            fos.close();
            putEntry(key, e);
            return;
        } catch (IOException e) {
        }
        boolean deleted = file.delete();
        if (!deleted) {
            VolleyLog.d("Could not clean up file %s", file.getAbsolutePath());
        }
    }

    /**
     * Removes the specified key from the cache if it exists.
     */
    @Override
    public synchronized void remove(String key) {
        boolean deleted = getFileForKey(key).delete();
        removeEntry(key);
        if (!deleted) {
            VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                    key, getFilenameForKey(key));
        }
    }

    /**
     * Creates a pseudo-unique filename for the specified cache key.
     * 通過給定的key,前半段的hashCode和後半段的hashCode連接起來
     * 作爲一個獨一無二的文件名
     * @param key The key to generate a file name for.
     * @return A pseudo-unique filename.
     */
    private String getFilenameForKey(String key) {
        int firstHalfLength = key.length() / 2;
        String localFilename = String.valueOf(key.substring(0, firstHalfLength).hashCode());
        localFilename += String.valueOf(key.substring(firstHalfLength).hashCode());
        return localFilename;
    }

    /**
     * Returns a file object for the given cache key.
     * 通過調用getFilenameForKey()方法來獲取相對路徑
     */
    public File getFileForKey(String key) {
        return new File(mRootDirectory, getFilenameForKey(key));
    }

    /**
     * Prunes the cache to fit the amount of bytes specified.
     * 從已有的緩存中清除數據
     * 直到掃出了一片neededSapce大小的空地爲止
     * @param neededSpace The amount of bytes we are trying to fit into the cache.
     */
    private void pruneIfNeeded(int neededSpace) {
        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
            return;
        }
        if (VolleyLog.DEBUG) {
            VolleyLog.v("Pruning old cache entries.");
        }

        long before = mTotalSize;
        int prunedFiles = 0;
        long startTime = SystemClock.elapsedRealtime();

        Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator();

        while (iterator.hasNext()) {

            Map.Entry<String, CacheHeader> entry = iterator.next();

            CacheHeader e = entry.getValue();

            boolean deleted = getFileForKey(e.key).delete();

            if (deleted) {
                mTotalSize -= e.size;
            } else {
               VolleyLog.d("Could not delete cache entry for key=%s, filename=%s",
                       e.key, getFilenameForKey(e.key));
            }
            iterator.remove();
            prunedFiles++;

            /**
             * 一直清除緩存
             * 直到存入這個neededSapce之後還有一小部分空餘的地方
             */
            if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
                break;
            }
        }

        if (VolleyLog.DEBUG) {
            VolleyLog.v("pruned %d files, %d bytes, %d ms",
                    prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime);
        }
    }

    /**
     * Puts the entry with the specified key into the cache.
     * 將目錄下指定的緩存加載到mEntries中去
     * 爲了方便之後對緩存的讀寫操作
     * 全部讀寫一遍放在內存裏面,對查詢什麼的都會方便很多
     *
     * @param key The key to identify the entry by.
     * @param entry The entry to cache.
     */
    private void putEntry(String key, CacheHeader entry) {
        if (!mEntries.containsKey(key)) {
            mTotalSize += entry.size;
        } else {
            CacheHeader oldEntry = mEntries.get(key);
            mTotalSize += (entry.size - oldEntry.size);
        }
        mEntries.put(key, entry);
    }

    /**
     * Removes the entry identified by 'key' from the cache.
     */
    private void removeEntry(String key) {
        CacheHeader entry = mEntries.get(key);
        if (entry != null) {
            mTotalSize -= entry.size;
            mEntries.remove(key);
        }
    }

    /**
     * Reads the contents of an InputStream into a byte[].
     * 從InputStream中讀取指定長度的數據
     * 
     */
    private static byte[] streamToBytes(InputStream in, int length) throws IOException {
        byte[] bytes = new byte[length];
        int count;
        int pos = 0;
        while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
            pos += count;
        }
        if (pos != length) {
            throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
        }
        return bytes;
    }

    /**
     * Handles holding onto the cache headers for an entry.
     */
    // Visible for testing.
    static class CacheHeader {
        /** 
         * The size of the data identified by this CacheHeader. (This is not
         * serialized to disk.
         * 
         * CacheHeader所表示的數據段的大小 
         */
        public long size;

        /** 
         * The key that identifies the cache entry. 
         * 這個key應該是request對應其緩存的唯一key
         */
        public String key;

        /** 
         * ETag for cache coherence.
         *
         */
        public String etag;

        /** 
         * Date of this response as reported by the server. 
         * 緩存起來的數據返回的日期
         */
        public long serverDate;

        /** 
         * The last modified date for the requested object. 
         * 最後一次更改的時間
         */
        public long lastModified;

        /** 
         * TTL for this record. 
         * ping時候返回的TTL=128的概念如下
         * TTL:生存時間
         * 指定數據報被路由器丟棄之前允許通過的網段數量。
         * TTL 是由發送主機設置的,以防止數據包不斷在 IP 互聯網絡上永不終止地循環。轉發 IP 數據包時,要求路由器至少將 TTL 減小 1。
         *  
         * 但是= =,注意這裏的和上面的那種不是一個概念,這裏只是模擬了上面的概念,但也是用來標誌緩存存活時間的。
         */
        public long ttl;

        /** 
         * Soft TTL for this record. 
         * 
         * 根據refreshNeeded()函數來看
         * 意思是需要更新緩存的時間點
         */
        public long softTtl;

        /** 
         * Headers from the response resulting in this cache entry. 
         * 用來指向上一次response的header
         */
        public Map<String, String> responseHeaders;

        private CacheHeader() { }

        /**
         * Instantiates a new CacheHeader object
         * @param key The key that identifies the cache entry
         * @param entry The cache entry.
         */
        public CacheHeader(String key, Entry entry) {
            this.key = key;
            this.size = entry.data.length;
            this.etag = entry.etag;
            this.serverDate = entry.serverDate;
            this.lastModified = entry.lastModified;
            this.ttl = entry.ttl;
            this.softTtl = entry.softTtl;
            this.responseHeaders = entry.responseHeaders;
        }

        /**
         * Reads the header off of an InputStream and returns a CacheHeader object.
         * 從InputStream中讀取數據並組建一個CacheHeader對象實例
         * @param is The InputStream to read from.
         * @throws IOException
         */
        public static CacheHeader readHeader(InputStream is) throws IOException {
            CacheHeader entry = new CacheHeader();
            int magic = readInt(is);
            if (magic != CACHE_MAGIC) {
                // don't bother deleting, it'll get pruned eventually
                throw new IOException();
            }
            entry.key = readString(is);
            entry.etag = readString(is);
            if (entry.etag.equals("")) {
                entry.etag = null;
            }
            entry.serverDate = readLong(is);
            entry.lastModified = readLong(is);
            entry.ttl = readLong(is);
            entry.softTtl = readLong(is);
            entry.responseHeaders = readStringStringMap(is);

            return entry;
        }

        /**
         * Creates a cache entry for the specified data.
         * 從CacheHeader轉換成Entry類的實例
         */
        public Entry toCacheEntry(byte[] data) {
            Entry e = new Entry();
            e.data = data;
            e.etag = etag;
            e.serverDate = serverDate;
            e.lastModified = lastModified;
            e.ttl = ttl;
            e.softTtl = softTtl;
            e.responseHeaders = responseHeaders;
            return e;
        }


        /**
         * Writes the contents of this CacheHeader to the specified OutputStream.
         * 將CacheHeader裏面的數據寫入指定的OutputStream中
         */
        public boolean writeHeader(OutputStream os) {
            try {
                writeInt(os, CACHE_MAGIC);
                writeString(os, key);
                writeString(os, etag == null ? "" : etag);
                writeLong(os, serverDate);
                writeLong(os, lastModified);
                writeLong(os, ttl);
                writeLong(os, softTtl);
                writeStringStringMap(responseHeaders, os);
                os.flush();
                return true;
            } catch (IOException e) {
                VolleyLog.d("%s", e.toString());
                return false;
            }
        }

    }

    /**
     * 繼承了FilterInputStream
     * 沒啥特別的= =
     */

    private static class CountingInputStream extends FilterInputStream {

        private int bytesRead = 0;

        private CountingInputStream(InputStream in) {
            super(in);
        }

        @Override
        public int read() throws IOException {
            int result = super.read();
            if (result != -1) {
                bytesRead++;
            }
            return result;
        }

        @Override
        public int read(byte[] buffer, int offset, int count) throws IOException {
            int result = super.read(buffer, offset, count);
            if (result != -1) {
                bytesRead += result;
            }
            return result;
        }
    }

    /*
     * Homebrewed simple serialization system used for reading and writing cache
     * headers on disk. Once upon a time, this used the standard Java
     * Object{Input,Output}Stream, but the default implementation relies heavily
     * on reflection (even for standard types) and generates a ton of garbage.
     * 
     */

    /**
     * Simple wrapper around {@link InputStream#read()} that throws EOFException
     * instead of returning -1.
     * 如果文件讀到了末尾直接拋出異常
     */
    private static int read(InputStream is) throws IOException {
        int b = is.read();
        if (b == -1) {
            throw new EOFException();
        }
        return b;
    }

    /**
     * 剛開始看到這裏的時候沒有明白是什麼意思= =
     * 就不明白了,好好的一個int類型的數據
     * 爲什麼非要分段寫入呢,一個字節一個字節的寫入
     * 後來查了資料才發現,OutputStream及其子類的write()方法
     * 一次都只能寫入一個byte,int類型有4個byte,分四次寫入沒什麼問題咯
     */

    static void writeInt(OutputStream os, int n) throws IOException {
        os.write((n >> 0) & 0xff);
        os.write((n >> 8) & 0xff);
        os.write((n >> 16) & 0xff);
        os.write((n >> 24) & 0xff);
    }

    static int readInt(InputStream is) throws IOException {
        int n = 0;
        n |= (read(is) << 0);
        n |= (read(is) << 8);
        n |= (read(is) << 16);
        n |= (read(is) << 24);
        return n;
    }

    static void writeLong(OutputStream os, long n) throws IOException {
        os.write((byte)(n >>> 0));
        os.write((byte)(n >>> 8));
        os.write((byte)(n >>> 16));
        os.write((byte)(n >>> 24));
        os.write((byte)(n >>> 32));
        os.write((byte)(n >>> 40));
        os.write((byte)(n >>> 48));
        os.write((byte)(n >>> 56));
    }

    static long readLong(InputStream is) throws IOException {
        long n = 0;
        n |= ((read(is) & 0xFFL) << 0);
        n |= ((read(is) & 0xFFL) << 8);
        n |= ((read(is) & 0xFFL) << 16);
        n |= ((read(is) & 0xFFL) << 24);
        n |= ((read(is) & 0xFFL) << 32);
        n |= ((read(is) & 0xFFL) << 40);
        n |= ((read(is) & 0xFFL) << 48);
        n |= ((read(is) & 0xFFL) << 56);
        return n;
    }

    static void writeString(OutputStream os, String s) throws IOException {
        byte[] b = s.getBytes("UTF-8");
        writeLong(os, b.length);
        os.write(b, 0, b.length);
    }

    static String readString(InputStream is) throws IOException {
        int n = (int) readLong(is);
        byte[] b = streamToBytes(is, n);
        return new String(b, "UTF-8");
    }

    static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException {
        if (map != null) {
            writeInt(os, map.size());
            for (Map.Entry<String, String> entry : map.entrySet()) {
                writeString(os, entry.getKey());
                writeString(os, entry.getValue());
            }
        } else {
            writeInt(os, 0);
        }
    }

    /**
     * 從InputStream中讀取key類型爲String,值類型也爲String的Map
     */ 
    static Map<String, String> readStringStringMap(InputStream is) throws IOException {
        int size = readInt(is);
        Map<String, String> result = (size == 0)
                ? Collections.<String, String>emptyMap()
                : new HashMap<String, String>(size);
        for (int i = 0; i < size; i++) {

            //將讀出來的byte[]轉換成String

            String key = readString(is).intern();
            String value = readString(is).intern();
            result.put(key, value);
        }
        return result;
    }
}

    涉及到緩存讀寫的這個實現類當時閱讀的時候還是花了不少時間的= =,人太笨了沒辦法orz,有什麼不妥的地方還望各位juju多多指教,小達感激不盡0.0。下面還有Request.java等類等着去解析呢,Volley中的主角要登場了╭(╯^╰)╮。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章