编程语言
首页 > 编程语言> > Android Volley 源码解析(二),探究缓存机制

Android Volley 源码解析(二),探究缓存机制

作者:互联网

前言

在上一篇文章中,带大家阅读了 Volley 网络请求的执行流程,算是对 Volley 有了一个比较清晰的认识,从这篇文章开始,我们开始针对 Volley 的某个功能进行深入地分析,慢慢将 Volley 的各项功能进行全面把握。

我们先从缓存这一块的内容开始入手,不过今天的缓存分析是是建立在上一篇源码分析的基础上的,还没有看过上一篇文章的朋友,建议先去阅读Android Volley 源码解析(一),网络请求的执行流程

一、Volley 缓存的总体设计

在开始细节分析之前,我们先来看下 Volley 缓存的设计,了解这个流程有助于我们对于缓存细节的把握。Volley 提供了一个 Cache 作为缓存的接口,封装了缓存的实体 Entry,以及一些常规的增删查操作。

public interface Cache {

    Entry get(String key);

    void put(String key, Entry entry);

    void initialize();

    /**
     * 使缓存中的 Entry 失效
     */
    void invalidate(String key, boolean fullExpire);

    void remove(String key);

    void clear();

    /**
     * 用户缓存的实体
     */
    class Entry {

        public byte[] data;

        public String etag;

        public long serverDate;

        public long lastModified;

        public long ttl;

        public long softTtl;

        public Map<String, String> responseHeaders = Collections.emptyMap();

        public List<Header> allResponseHeaders;

        /** 判断 Entry 是否过期. */
        public boolean isExpired() {
            return this.ttl < System.currentTimeMillis();
        }

        /** 判断 Entry 是否需要刷新. */
        public boolean refreshNeeded() {
            return this.softTtl < System.currentTimeMillis();
        }
    }

}

Entry 里面主要是放网络响应的原始数据 data、跟缓存相关的属性以及对应的响应头,作为缓存的一个实体。Cache 的具体实现类是 DiskBaseCache,它实现了 Cache 接口,并实现了响应的方法,那我们就来看看 DiskBaseCache 的设计吧,我们先看下 DiskBaseCache 中的一个静态内部类 CacheHeader.

 static class CacheHeader {

        long size;

        final String key;

        final String etag;

        final long serverDate;

        final long lastModified;

        final long ttl;

        final long softTtl;

        final List<Header> allResponseHeaders;

        private CacheHeader(String key, String etag, long serverDate, long lastModified, long ttl,
                           long softTtl, List<Header> allResponseHeaders) {
            this.key = key;
            this.etag = ("".equals(etag)) ? null : etag;
            this.serverDate = serverDate;
            this.lastModified = lastModified;
            this.ttl = ttl;
            this.softTtl = softTtl;
            this.allResponseHeaders = allResponseHeaders;
        }

        CacheHeader(String key, Entry entry) {
            this(key, entry.etag, entry.serverDate, entry.lastModified, entry.ttl, entry.softTtl,
                    getAllResponseHeaders(entry));
            size = entry.data.length;
        }
    }

DiskBaseCache 的设计很巧妙,它在内部放入了一个静态内部类 CacheHeader,我们可以发现这个类跟 Cache 的 Entry 非常像,是不是会觉得好像有点多余,Volley 之所以要这样设计,主要是为了缓存的合理性。我们知道每一个应用都是有一定内存限制的,程序占用了过高的内存就容易出现 OOM(Out of Memory),如果每一个请求都原封不动的把所有的信息都缓存到内存中,这样是非常占内存的。

我们可以发现 CacheHeader 和 Entry 最大的区别,其实就是是否有 byte[] data 这个属性,data 代表网络响应的元数据,是返回的内容中最占地方的东西,所以 DiskBaseCache 重新抽象了一个不包含 data 的 CacheHeader,并将其缓存到内存中,而 data 部分便存储在磁盘缓存中,这样就能最大程度的利用有限的内存空间。代码如下:

 BufferedOutputStream fos = new BufferedOutputStream(createOutputStream(file));
    CacheHeader e = new CacheHeader(key, entry);
    boolean success = e.writeHeader(fos);
    // 将 entry.data 写入磁盘中
    fos.write(entry.data);
    fos.close();
    // 将 Cache 缓存到内存中
    putEntry(key, e);

二、DiskBaseCache 的具体实现

看完了 Volley 的缓存设计,我们接着看 DiskBaseCache 的具体实现。

2.1 初始化缓存

 // 内存缓存的目录
  private final File mRootDirectory;

  public DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) {
      mRootDirectory = rootDirectory;
      mMaxCacheSizeInBytes = maxCacheSizeInBytes;
  }

  @Override
  public synchronized void initialize() {
      // 如果 mRootDirectroy 不存在,则进行创建
      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;
      }
      // 遍历 mRootDirectory 中的所有文件
      for (File file : files) {
          try {
              long entrySize = file.length();
              CountingInputStream cis = new CountingInputStream(
                      new BufferedInputStream(createInputStream(file)), entrySize);
              // 将对应的文件缓存到内存中
              CacheHeader entry = CacheHeader.readHeader(cis);
              entry.size = entrySize;
              putEntry(entry.key, entry);
          } catch (IOException e) {
              file.delete();
          }
      }
  }

通过外部传入的 rootDirectory 和 maxCacheSizeInBytes 构造 DiskBaseCache 的实例,mRootDirectory 代表我们内存缓存的目录,maxCacheSizeInBytes 代表磁盘缓存的大小,默认是 5M。如果 mRootDirectory 为 null,则进行创建,然后将 mRootDirectory 中的所有文件进行内存缓存。

2.2 put() 方法的实现

    @Override
    public synchronized void put(String key, Entry entry) {
        pruneIfNeeded(entry.data.length);
        File file = getFileForKey(key);
        try {
            BufferedOutputStream fos = new BufferedOutputStream(createOutputStream(file));
            CacheHeader e = new CacheHeader(key, entry);
            boolean success = e.writeHeader(fos);
            fos.write(entry.data);
            fos.close();
            putEntry(key, e);
            return;
        } catch (IOException e) {
        }
    }

    private void pruneIfNeeded(int neededSpace) {
        // 如果内存还够用,就直接 return.
        if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) {
            return;
        }

        long before = mTotalSize;
        int prunedFiles = 0;

        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;
            } 
            iterator.remove();
            prunedFiles++;

            // 如果删除文件后,存储空间已经够用了,就停止循环
            if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) {
                break;
            }
        }
    }

标签:DiskBaseCache,缓存,源码,key,entry,Volley,Android,public
来源: https://blog.csdn.net/qq_43257419/article/details/88410163