Android系统中WebView的本地缓存重写

在现在的app中,越来越多的公司开始使用webview来进行一些活动页面的展示,甚至一些公司开始使用webview做为主要显示组件,把所有的内容都使用H5来呈现,这样一来,就对WebView的加载速度开始有越来越高的要求,我们要讨论的是webview的原本缓存机制所存在的弊端和如何复写WebView的缓存机制。

首先说一下webview的自带缓存机制的弊端:

webview的自带缓存机制是无差别缓存,也就是说,不管是页面,样式还是图片,都会缓存到本地,刷新webview的缓存一般分为以下几种:

LOAD_CACHE_ONLY:不使用网络,只读取本地缓存数据

LOAD_DEFAULT:根据cache-control决定是否从网络上取数据。

LOAD_CACHE_NORMAL:APIlevel17中已经废弃,从APIlevel11开始作用同LOAD_DEFAULT模式

LOAD_NO_CACHE:不使用缓存,只从网络获取数据.

LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。

反正不管以上几种具体情况如何,都肯定不是我们想要的。

我们想要的机制是:

1.缓存自己想要缓存的内容。

2.指定一个缓存策略,在需要的时候重新去服务器获取最新数据

于是我想到了以下方法

重写
WebViewClient.shouldInterceptRequest(WebView view,WebResourceRequest request)
方法

首先需要新建一个类,继承WebViewClient:

public class DVDWebViewClient extends WebViewClient

然后实现

public WebResourceResponse shouldInterceptRequest(WebView view,WebResourceRequest request)

方法

shouldInterceptRequest方法会将所有页面的资源URL都一一列举出来,这样一来就好办了,我们似乎只需要缓存自己想要缓存的url就可以了。

然后事实是不是这样的呢?

@Override
    public WebResourceResponse shouldInterceptRequest(WebView view,
                                                      WebResourceRequest request) {
        //获取本地的URL主域名
        String curDomain = request.getUrl().getHost();
        //这行LOG可以不看
        if (BuildConfig.DEBUG) {
            Log.d(LOG_TAG, "curDomain " + curDomain + " request headers " + request.getRequestHeaders());
            for (Map.Entry<String, String> entry : request.getRequestHeaders().entrySet()) {
                Log.d(LOG_TAG, "key=" + entry.getKey() + " #####value=" + entry.getValue() + "\n");
            }
        }
        //取不到domain就直接返回,把接下俩的动作交给webview自己处理
        if (curDomain == null || !isPicUrl(curDomain)) {
            return null;
        }
        if (BuildConfig.DEBUG) {
            Log.d(LOG_TAG, "shouldInterceptRequest url " + request.getUrl().toString());
        }
        //读取当前webview正准备加载URL资源
        String url = request.getUrl().toString();
        try {
           //根据资源url获取一个你要缓存到本地的文件名,一般是URL的MD5
            String resFileName = getResourcesFileName(url);
            if (resFileName == null || "".equals(resFileName)) {
                return null;
            }
           //这里是处理本地缓存的URL,缓存到本地,或者已经缓存过的就直接返回而不去网络进行加载
            this.dvdUrlCache.register(url, getResourcesFileName(url),
                    request.getRequestHeaders().get("Accept"), "UTF-8", DVDUrlCache.ONE_MONTH);
            return this.dvdUrlCache.load(url);
        } catch (Exception e) {
            Log.e(LOG_TAG, "", e);
        }
        return null;
    }

接下来我们看下DVDUrlCache的实现:

DVDUrlCache主要做了这么几件事:

1.封装一个内部类CacheEntry,做一些基本信息存储

private static class CacheEntry {
       //用作存储的URL
        public String url;
        //本地保存的文件名称
        public String fileName;
        //标记资源的头部,通过request参数取回
        String mimeType;
        //需要缓存的资源文件的编码
        public String encoding;
        //缓存最大有效时间
        long maxAgeMillis;

        private CacheEntry(String url, String fileName,
                           String mimeType, String encoding, long maxAgeMillis) {
            this.url = url;
            this.fileName = fileName;
            this.mimeType = mimeType;
            this.encoding = encoding;
            this.maxAgeMillis = maxAgeMillis;
        }
    }

接下来是类的构造放方法以及需要映射的map

//Key 为URL
private Map<String, CacheEntry> cacheEntries = new HashMap<>();
//缓存路径的根目录
    private File rootDir = null;
    DVDUrlCache() {
//获取本地缓存路径,这个请在调试中自行修改
        this.rootDir = DiskUtil.getDiskCacheDir(DVDApplicationContext.getInstance().getApplicationContext());
    }
//资源注册,参考DVDWebViewClient的调用
public void register(String url, String cacheFileName,
                         String mimeType, String encoding,
                         long maxAgeMillis) {
        CacheEntry entry = new CacheEntry(url, cacheFileName, mimeType, encoding, maxAgeMillis);
        this.cacheEntries.put(url, entry);
    }

然后是核心内容

public WebResourceResponse load(final String url) {
        try {
            final CacheEntry cacheEntry = this.cacheEntries.get(url);
            if (cacheEntry == null) {
                return null;
            }

            final File cachedFile = new File(this.rootDir.getPath() + File.separator + cacheEntry.fileName);
            if (BuildConfig.DEBUG) {
                Log.d(LOG_TAG, "cachedFile is " + cachedFile);
            }
            if (cachedFile.exists() && isReadFromCache(url)) {
                //还没有下载完,在快速切换URL的时候,可能会有很多task并没有及时完成,所以这里需要一个map用于存储正在下载的URL,下载完成后需要移除相应的task
                if (queueMap.containsKey(url)) {               
                    return null;
                }
//过期后直接删除本地缓存内容
                long cacheEntryAge = System.currentTimeMillis() - cachedFile.lastModified();
                if (cacheEntryAge > cacheEntry.maxAgeMillis) {
                    cachedFile.delete();
                    if (BuildConfig.DEBUG) {
                        Log.d(LOG_TAG, "Deleting from cache: " + url);
                    }
                    return null;
                }
                //cached file exists and is not too old. Return file.
                if (BuildConfig.DEBUG) {
                    Log.d(LOG_TAG, url + " ### cache file : " + cachedFile.getAbsolutePath());
                }
                return new WebResourceResponse(
                        cacheEntry.mimeType, cacheEntry.encoding, new FileInputStream(cachedFile));
            } else {
                if (!queueMap.containsKey(url)) {
                    queueMap.put(url, new Callable<Boolean>() {
                        @Override
                        public Boolean call() throws Exception {
                            return downloadAndStore(url, cacheEntry);
                        }
                    });
                    final FutureTask<Boolean> futureTask = ThreadPoolManager.getInstance().addTaskCallback(queueMap.get(url));
                    ThreadPoolManager.getInstance().addTask(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if (futureTask.get()) {
                                    if (BuildConfig.DEBUG) {
                                        Log.d(LOG_TAG, "remove " + url);
                                    }
                                    queueMap.remove(url);
                                }
                            } catch (InterruptedException | ExecutionException e) {
                                Log.d(LOG_TAG, "", e);
                            }
                        }
                    });
                }
            }
        } catch (Exception e) {
            Log.d(LOG_TAG, "Error reading file over network: ", e);
        }
        return null;
    }

//这个方法是资源下载
    private boolean downloadAndStore(final String url, final CacheEntry cacheEntry)
            throws IOException {
        FileOutputStream fileOutputStream = null;
        InputStream urlInput = null;
        try {
            URL urlObj = new URL(url);
            URLConnection urlConnection = urlObj.openConnection();
            urlInput = urlConnection.getInputStream();
            String tempFilePath = DVDUrlCache.this.rootDir.getPath() + File.separator + cacheEntry.fileName + ".temp";
            File tempFile = new File(tempFilePath);
            fileOutputStream = new FileOutputStream(tempFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = urlInput.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }
            fileOutputStream.flush();
            File lastFile = new File(tempFilePath.replace(".temp", ""));
            boolean renameResult = tempFile.renameTo(lastFile);
            if (!renameResult) {
                Log.w(LOG_TAG, "rename file failed, " + tempFilePath);
            }
//            Log.d(LOG_TAG, "Cache file: " + cacheEntry.fileName + " stored. ");
            return true;
        } catch (Exception e) {
            Log.e(LOG_TAG, "", e);
        } finally {
            if (urlInput != null) {
                urlInput.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
        return false;
    }

    private boolean isReadFromCache(String url) {
        return true;
    }

完整的DVDURLCache代码,方便大家直接copy

package com.davdian.seller.util.WebUtil;

import android.util.Log;
import android.webkit.WebResourceResponse;
import com.davdian.seller.BuildConfig;
import com.davdian.seller.global.DVDApplicationContext;
import com.davdian.seller.util.DiskUtil;
import com.davdian.seller.util.ThreadPoolManager;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 只缓存图片的自定义接口
 * <p>
 * Created by hongminghuangfu on 16/9/3.
 */
public class DVDUrlCache {

    private static final String LOG_TAG = "DVDUrlCache";

    private static final long ONE_SECOND = 1000L;
    private static final long ONE_MINUTE = 60L * ONE_SECOND;
    static final long ONE_HOUR = 60 * ONE_MINUTE;
    static final long ONE_DAY = 24 * ONE_HOUR;
    static final long ONE_MONTH = 30 * ONE_DAY;


    private static final LinkedHashMap<String, Callable<Boolean>> queueMap = new LinkedHashMap<>();


    private static class CacheEntry {
        public String url;
        public String fileName;
        String mimeType;
        public String encoding;
        long maxAgeMillis;

        private CacheEntry(String url, String fileName,
                           String mimeType, String encoding, long maxAgeMillis) {
            this.url = url;
            this.fileName = fileName;
            this.mimeType = mimeType;
            this.encoding = encoding;
            this.maxAgeMillis = maxAgeMillis;
        }
    }

    private Map<String, CacheEntry> cacheEntries = new HashMap<>();
    private File rootDir = null;

    DVDUrlCache() {
//本地缓存路径,请在调试中自行修改
        this.rootDir = DiskUtil.getDiskCacheDir(DVDApplicationContext.getInstance().getApplicationContext());
    }

    public void register(String url, String cacheFileName,
                         String mimeType, String encoding,
                         long maxAgeMillis) {
        CacheEntry entry = new CacheEntry(url, cacheFileName, mimeType, encoding, maxAgeMillis);
        this.cacheEntries.put(url, entry);
    }

    public WebResourceResponse load(final String url) {
        try {
            final CacheEntry cacheEntry = this.cacheEntries.get(url);
            if (cacheEntry == null) {
                return null;
            }

            final File cachedFile = new File(this.rootDir.getPath() + File.separator + cacheEntry.fileName);
            if (BuildConfig.DEBUG) {
                Log.d(LOG_TAG, "cachedFile is " + cachedFile);
            }
            if (cachedFile.exists() && isReadFromCache(url)) {
                //还没有下载完
                if (queueMap.containsKey(url)) {                
                    return null;
                }
                long cacheEntryAge = System.currentTimeMillis() - cachedFile.lastModified();
                if (cacheEntryAge > cacheEntry.maxAgeMillis) {
                    cachedFile.delete();
                    if (BuildConfig.DEBUG) {
                        Log.d(LOG_TAG, "Deleting from cache: " + url);
                    }
                    return null;
                }
                //cached file exists and is not too old. Return file.
                if (BuildConfig.DEBUG) {
                    Log.d(LOG_TAG, url + " ### cache file : " + cachedFile.getAbsolutePath());
                }
                return new WebResourceResponse(
                        cacheEntry.mimeType, cacheEntry.encoding, new FileInputStream(cachedFile));
            } else {
                if (!queueMap.containsKey(url)) {
                    queueMap.put(url, new Callable<Boolean>() {
                        @Override
                        public Boolean call() throws Exception {
                            return downloadAndStore(url, cacheEntry);
                        }
                    });
                    final FutureTask<Boolean> futureTask = ThreadPoolManager.getInstance().addTaskCallback(queueMap.get(url));
                    ThreadPoolManager.getInstance().addTask(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                if (futureTask.get()) {
                                    if (BuildConfig.DEBUG) {
                                        Log.d(LOG_TAG, "remove " + url);
                                    }
                                    queueMap.remove(url);
                                }
                            } catch (InterruptedException | ExecutionException e) {
                                Log.d(LOG_TAG, "", e);
                            }
                        }
                    });
                }
            }
        } catch (Exception e) {
            Log.d(LOG_TAG, "Error reading file over network: ", e);
        }
        return null;
    }

    private boolean downloadAndStore(final String url, final CacheEntry cacheEntry)
            throws IOException {
        FileOutputStream fileOutputStream = null;
        InputStream urlInput = null;
        try {
            URL urlObj = new URL(url);
            URLConnection urlConnection = urlObj.openConnection();
            urlInput = urlConnection.getInputStream();
            String tempFilePath = DVDUrlCache.this.rootDir.getPath() + File.separator + cacheEntry.fileName + ".temp";
            File tempFile = new File(tempFilePath);
            fileOutputStream = new FileOutputStream(tempFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = urlInput.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }
            fileOutputStream.flush();
            File lastFile = new File(tempFilePath.replace(".temp", ""));
            boolean renameResult = tempFile.renameTo(lastFile);
            if (!renameResult) {
                Log.w(LOG_TAG, "rename file failed, " + tempFilePath);
            }
//            Log.d(LOG_TAG, "Cache file: " + cacheEntry.fileName + " stored. ");
            return true;
        } catch (Exception e) {
            Log.e(LOG_TAG, "", e);
        } finally {
            if (urlInput != null) {
                urlInput.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        }
        return false;
    }

    private boolean isReadFromCache(String url) {
        return true;
    }

}

ThreadPoolManager很简单:

package com.davdian.seller.util;

import android.util.Log;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * 线程池
 * <p>
 * Created by hongminghuangfu on 16/9/9.
 */
public class ThreadPoolManager {

    private static final String LOG_TAG = "ThreadPoolManager";
    private static final ThreadPoolManager instance = new ThreadPoolManager();

    private ExecutorService threadPool = Executors.newFixedThreadPool(100);

    public static ThreadPoolManager getInstance() {
        return instance;
    }

    /**
     * @param runnable 不返回执行结果的异步任务
     */
    public void addTask(Runnable runnable) {
        try {
            if (runnable != null) {
                threadPool.execute(runnable);
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "", e);
        }
    }

    /**
     * @param callback 异步任务
     * @return 你可以获取相应的执行结果
     */
    public FutureTask addTaskCallback(Callable<Boolean> callback) {
        if (callback == null) {
            return null;
        } else {
            FutureTask futureTask = new FutureTask<>(callback);
            threadPool.submit(futureTask);
            return futureTask;
        }

    }
// 这是一个demo,如果你看不懂,可以打开跑一下
//    public static void main(String args[]) {
//        FutureTask ft = ThreadPoolManager.getInstance().addTaskCallback(new Callable<Object>() {
//            @Override
//            public Object call() throws Exception {
//                int sum = 0;
//                for (int i = 0; i < 1000; i++) {
//                    sum++;
//                }
//                return sum;
//            }
//        });
//        try {
//            System.out.println("执行结果是:" + ft.get());
//        } catch (InterruptedException | ExecutionException e) {
//            e.printStackTrace();
//        }
//
//    }
}

相关推荐