当前位置:首页 > 安卓源码 > 技术博客 >

如何设计Volley的缓存功能增大它的命中率

时间:2017-02-22 00:16 来源:互联网 作者:源码搜藏 浏览: 收藏 挑错 推荐 打印

下面是 DiskBasedCache#pruneIfNeeded() 源码: [代码]java代码: ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 private void pruneIfNeeded( int neededSpace) { if ((mTotalSize + neededSpace) mMaxCacheSizeInByte 下面是DiskBasedCache#pruneIfNeeded()源码

[代码]java代码:


private void pruneIfNeeded(int neededSpace) {
    if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
        return;
    }
 
    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 {
    //print log
        }
        iterator.remove();
        prunedFiles++;
        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
            break;
        }
    }
}</string,></map.entry<string,>
在缓存内容可能超过缓存文件夹的大小时,删除的逻辑是直接遍历header删除。这个时候删除的文件有可能是我们上一次请求时刚刚保存下来的,屁股都还没坐稳呢,现在立即删掉,有点舍不得啊。 
    如果遍历的时候,判断一下,首先删除超过缓存有效期的(过期缓存),其次按照LRU算法,删除最久未使用的,岂不是更合适

如何设计Volley的缓存功能增大它的命中率 转载https://www.codesocang.com/appboke/34941.html

技术博客阅读排行

最新文章