SoFunction
Updated on 2025-04-09

Android WebView Cache Detailed Explanation

Android WebView cache detailed explanation

1. Two cache types:

Page caching: html, JS, CSS and other pages or resource data when loading a web page. These cache resources are generated due to the behavior of the browser. Developers can only indirectly affect the behavior of the browser by configuring HTTP response headers. The cached index is placed in: /data/data//databases The corresponding file is placed in: /data/data/package_name/cache/webviewCacheChromunm
Data cache: It is divided into AppCache and DOM Storage. These cache resources can be controlled by developers.

AppCache:

We can selectively buffer everything in the web browser, from pages, pictures to scripts, css, etc. It is especially useful when it comes to CSS and JavaScript files applied to multiple pages of a website. Its size is usually 5M at present. On Android, you need to manually enable (setAppCacheEnabled), and set the path (setAppCachePath) and capacity (setAppCacheMaxSize), and use it in android to save AppCache data!

DOM Storage:

Store some simple data that can be solved with key/value pairs. According to the scope of action, there are two types of data, Session Storage and Local Storage, which are used for session-level storage (the page disappears if closed) and local storage (the data will never expire unless it is actively deleted). In Android, DOM Storage (setDomStorageEnabled) can be manually enabled, and the storage path (setDatabasePath) Webkit in Android will generate two files for DOMStorage (my_path/localstorage/http_blog.csdn.net_0.localstorage and my_path/)

2. 5 cache modes of WebView:

LOAD_NO_CACHE: No cache is used, only data is obtained from the network
LOAD_CACHE_ONLY:   Don’t use the network, only local cached data is read
LOAD_DEFAULT: Decide whether to retrieve data from the network based on cache-control
LOAD_CACHE_NORMAL: API level 17 has been abandoned, and it has the same function as LOAD_DEFAULT mode since API level 11
LOAD_CACHE_ELSE_NETWORK, as long as it has it locally, whether it expires or not, or no-cache, the cached data is used.

3. Settings of WebView cache mode:

().setCacheMode(WebSettings.LOAD_NO_CACHE);// or other modes

The cache strategy according to the requirements can be: determine whether there is a network. There is: use LOAD_DEFAULT/LOAD_NO_CACHE

None: Use LOAD_CACHE_ELSE_NETWORK

4. Delete cached data:

().setCacheMode(WebSettings.LOAD_NO_CACHE);
 (true);
 ();
 ();
/**
  * Clear the WebView cache
  */
public void clearWebViewCache(){

  //Clean the Webview cache database  try {
    deleteDatabase("");
    deleteDatabase("");
  } catch (Exception e) {
    ();
  }

  //WebView cache files  File appCacheDir = new File(getFilesDir().getAbsolutePath()+APP_CACAHE_DIRNAME);
  (TAG, "appCacheDir path="+());

  File webviewCacheDir = new File(getCacheDir().getAbsolutePath()+"/webviewCache");
  (TAG, "webviewCacheDir path="+());

  //Delete the webview cache directory  if(()){
    deleteFile(webviewCacheDir);
  }
  //Delete the webview cache cache directory  if(()){
    deleteFile(appCacheDir);
  }
}  

Thank you for reading, I hope it can help you. Thank you for your support for this site!