1. Introduction to H5 cache mechanism H5, or HTML5, is a new generation of HTML standard, adding many new features. Offline storage (also known as cache mechanism) is one of the most important features. H5 introduces offline storage, which means that web applications can be cached and accessed when there is no Internet connection. H5 application cache brings three advantages to applications:
According to the standards, H5 has a total of 6 cache mechanisms so far, some of which have existed before and some of which have been newly added to H5.
Below we first analyze the principles, usage and characteristics of various cache mechanisms; then we look at how to use appropriate cache mechanisms to improve Web loading performance in response to the needs of Android mobile Web performance loading optimization. 2. Analysis of the H5 cache mechanism principle 2.1 Browser Cache Mechanism The browser cache mechanism refers to the mechanism that controls file cache through fields such as Cache-Control (or Expires) and Last-Modified (or Etag) in the HTTP protocol header. This should be the earliest cache mechanism in the WEB, which is implemented in the HTTP protocol. It is a little different from cache mechanisms such as Dom Storage and AppCache, but they are essentially the same. It can be understood that one is implemented at the protocol layer and the other is implemented at the application layer. Cache-Control is used to control the effective length of time that files are cached locally. The most common example is the server reply: Cache-Control:max-age=600 means that the file should be cached locally and the effective length is 600 seconds (from the time the request is made). In the next 600 seconds, if there is a request for this resource, the browser will not issue an HTTP request, but will directly use the locally cached file. Last-Modified is the last update time of the file on the server. The next time you request a file, if the file cache has expired, the browser sends this time to the server through the If-Modified-Since field, and the server compares the timestamp to determine whether the file has been modified. If it has not been modified, the server returns 304 to tell the browser to continue using the cache; if it has been modified, it returns 200 and returns the latest file. Cache-Control is usually used together with Last-Modified. One is used to control the cache validity period, and the other is used to query the service for updates after the cache expires. Cache-Control also has a field with the same function: Expires. The value of Expires is an absolute time point, such as: Expires: Thu, 10 Nov 2015 08:45:11 GMT, which means that the cache is valid before this time point. Expires is a field in the HTTP1.0 standard, and Cache-Control is a new field in the HTTP1.1 standard. They have the same function, both of which control the effective time of the cache. When these two fields appear at the same time, Cache-Control is highly optimized. Etag is also a field for identifying files, just like Last-Modified. The difference is that the value of Etag is a characteristic string for identifying files. When querying the server whether a file has been updated, the browser sends the characteristic string to the server through the If-None-Match field, and the server matches the file's characteristic string to determine whether the file has been updated. If there is no update, the response packet is 304, and if there is an update, the response packet is 200. Etag and Last-Modified can be used one or both at the same time according to needs. When both are used at the same time, as long as one of the base conditions is met, the file is considered not to have been updated. There are two special cases:
Below are screenshots of different requests and responses for a resource file using the developer tools built into the Google Chrome browser (you can also use other browsers + packet capture tools). *** Request: 200 Request within the cache validity period: 200 (from cache) Request after cache expiration: 304 (Not Modified) Generally, browsers will store cache records and cache files in the local Cache folder. If an Android app uses Webview, the cached file records and file contents will be stored in the data directory of the current app. Analysis: Cache-Control and Last-Modified are generally used on static resource files on the Web, such as JS, CSS, and some image files. Setting the cache properties of resource files is very meaningful for improving the loading speed of resource files and saving traffic, especially in mobile network environments. But the question is: how to set the effective duration of the cache? If it is set too short, the cache will not be used; if it is set too long, when the resource file is updated, if the browser has cache, it will not be able to retrieve the latest file in time. Last-Modified requires a query request to the server to know whether the resource file has been updated. Although the server may return 304 to indicate that there is no update, there is still a request process. For mobile networks, this request may be time-consuming. There is a saying called "eliminate 304", which means optimizing 304 requests. Packet capture shows that for requests with the if-Modified-Since field, if the server responds with a 304 packet and the response contains the Cache-Control:max-age or Expires field, the cache validity period of the file will be updated, that is, the cache of the file will be valid again. If a request is made again after the 304 response, the cached file will be used directly, and the server will no longer be queried to see if the file has been updated, unless the new cache time expires again. In addition, Cache-Control and Last-Modified are mechanisms of the browser kernel, which are generally implemented in a standard way and cannot be changed or set. Taking QQ Browser X5 as an example, Cache-Control and Last-Modified cache cannot be disabled. The cache capacity is 12MB, regardless of HOST, and expired cache will be cleared first. If none of them have expired, the earliest cache or the one that expires the soonest or has the largest file size should be cleared first; expired cache may still be valid, and clearing the cache will cause the resource files to be re-pulled. In addition, browsers, such as X5, do not verify the contents of cached files when using them, which makes it possible for the contents of cached files to be modified. The analysis found that the browser's cache mechanism is not a very perfect cache mechanism. The best cache mechanism should be like this:
In actual applications, in order to solve the problem of difficult to set Cache-Control cache duration and to "eliminate 304", the Web front-end adopts the following method:
In this way, the purpose of using the cached file when it is not updated and using the latest file when it is updated is achieved. This is the purpose of using the cached file when it is not updated. This is the purpose of using the cached file when it is ... 2.2 Dom Storage Storage Mechanism DOM storage is a general term for a set of storage-related features first introduced in the Web Applications 1.0 specification. It has now been separated and developed into an independent W3C Web Storage specification. DOM storage is designed to provide a larger storage capacity, safer, and more convenient storage method, which can replace the traditional method of storing some information that does not need to be known to the server in cookies. The above paragraph is the official description of the Dom Storage storage mechanism. It seems that the Dom Storage mechanism is similar to Cookies, but has some advantages. Dom Storage is provided by storing string Key/Value pairs, and provides 5MB (may be different for different browsers, divided by HOST) of storage space (Cookies are only 4KB). In addition, the data stored in Dom Storage is local, unlike Cookies, which will be sent to the server every time a page is requested. DOM Storage is divided into sessionStorage and localStorage. The usage of localStorage object and sessionStorage object is basically the same, the difference between them is the scope of application. sessionStorage is used to store data related to the page, and it cannot be used after the page is closed. LocalStorage is persistent and can be used even after the page is closed. Dom Storage provides the following storage interfaces:
sessionStorage is a global object that maintains storage space that is valid during a page session. The page session period lasts as long as the browser is open. The page session also persists when the page is reloaded or restored. Each time a new page is opened in a new tab or window, a new session is initialized.
When the browser is accidentally refreshed, some temporary data should be saved and restored. The sessionStorage object is most useful in handling this situation. For example, restore the data we have filled in the form. Copy the above code to the session_storage.html page (you can also download it directly from the attachment), open it with different pages or windows of Google Chrome browser, enter different text in the input box, click "Save", and then refresh them respectively. Each page or window displays the content entered in the current page, and they do not affect each other. If you close the page and reopen it, the content you entered last time will be gone. The interface and usage of Local Storage are the same as Session Storage, the only difference is that the data saved in Local Storage is persistent. When the current PAGE is closed (after the Page Session ends), the saved data still exists. When the PAGE is reopened, the last saved data can be obtained. In addition, Local Storage is global. If two PAGEs are opened at the same time, they will share a copy of the stored data. If the data is modified in one PAGE, it can be perceived in the other PAGE.
Copy the above code to the local_storage.html page and open it with a browser. The value of pageLoadCount is 1. Close the page and reopen it. The value of pageLoadCount is 2. This is because the last value has been saved. Use two pages to open local_storage.html at the same time and refresh them alternately. It is found that the two pages share the same pageLoadCount. Analysis: Dom Storage provides a more flexible data storage method for the Web, with larger storage space (relative to Cookies), simpler usage, and convenient for storing some temporary data on the server or locally. From the interface provided by DomStorage, DomStorage is suitable for storing relatively simple data. If you want to store structured data, you may need to use JASON to convert the object to be stored into a JASON string. It is not suitable for storing complex data or data that requires a large storage space, nor is it suitable for storing static files. In the Android embedded Webview, you need to enable Dom Storage through the Webview settings interface.
To use Android as an analogy, the Web's Dom Storage mechanism is similar to Android's SharedPreference mechanism. 2.3 Web SQL Database Storage Mechanism H5 also provides a SQL-based database storage mechanism for storing structured data suitable for the database. According to official standard documents, the Web SQL Database storage mechanism is no longer recommended and will no longer be maintained in the future. Instead, AppCache and IndexedDB are recommended. Currently, most mainstream browsers (click here to view browser support) still support the Web SQL Database storage mechanism. The Web SQL Database storage mechanism provides a set of APIs for Web Apps to create, store, and query databases. The following is a simple example to demonstrate the use of Web SQL Database.
Copy the above code into sql_database.html and open it with a browser to see the following content. The official recommendation is that browsers should set certain limits on the database storage space for each host when implementing it. The recommended default quota is 5MB (per host). When the upper limit is reached, you can apply for more storage space. In addition, the SQL Database implementation of mainstream browsers is based on SQLite. Analysis: The main advantage of SQL Database is that it can store complex data and fully utilize the advantages of the database to easily add, delete, modify, and query data. Due to the complexity of SQL syntax, it is more troublesome to use. SQL Database is also not very suitable for caching static files. In the Android embedded Webview, you need to enable SQL Database through the Webview settings interface and set the storage path of the database file.
The Android system also uses a large number of databases to store data, such as contacts, short messages, etc. The database format is also SQLite. Android also provides an API to operate SQLite. The Web SQL Database storage mechanism provides this native function to Web Apps by providing a set of APIs and using the browser to implement it. #p# 2.4 Application Cache Mechanism Application Cache (AppCache for short) seems to be a cache mechanism developed to support offline use of Web Apps. Its cache mechanism is similar to the browser's cache (Cache-Control and Last-Modified) mechanism, both of which cache files in units, and the files have a certain update mechanism. However, AppCache is a supplement to the browser's cache mechanism, not a replacement. Let's first take an official example from W3C to explain the usage and function of the AppCache mechanism.
The above HTML document references an external JS file and a GIF image file, and references a file ending with appcache through the manifest attribute in its HTML header. We open this HTML link in Google Chrome browser, JS functions normally and the image is displayed normally. Disable the network, close the browser and reopen the link, and we find that JS works normally and the image is displayed normally. Of course, it may also be the browser cache that plays a role. We can disable the network and try again after the browser cache of the file expires, and we find that the HTML page is also normal. Through the tools that come with the Google Chrome browser, we can view the cached AppCache (divided by HOST). The cache in the screenshot above is the AppCache of the HTML page we just opened. From the screenshot, we can see that the HTML page and the JS and GIF image files referenced by the HTML are all cached; in addition, the appcache file referenced by the manifest attribute in the HTML header is also cached. There are two key points in the principle of AppCache: manifest attributes and manifest files. HTML references the manifest file through the manifest attribute in the header. The manifest file, which is the file ending with appcache above, is a normal file that lists the files that need to be cached. The manifest file in the screenshot above is the manifest file referenced by the HTML code. The file is relatively simple. The first line is the keyword, and the second and third lines are the file paths to be cached (relative paths). This is just the simplest manifest file. A complete manifest file also includes other keywords and content. The HTML that references the manifest file and the files to be cached listed in the manifest file will eventually be cached by the browser. A complete manifest file includes three sections, similar to the sections in the ini configuration file in Windows, but without the square brackets.
A complete manifest file, such as:
In general, when a browser first loads an HTML file, it parses the manifest attribute, reads the manifest file, gets the list of files to be cached under Section: CACHE MANIFEST, and then caches the files. Are AppCache's cache files stored separately from the browser's cache files, or are they the same? They should be separate, because AppCache also has a local space limit of 5MB (per host). AppCache also has an update mechanism after it is loaded and generated. If the cached files need to be updated, the manifest file needs to be updated. Because the browser will not only use the cache by default when loading the next time, but will also check whether the manifest file has been modified (byte by byte) in the background. If there is a modification, the manifest file will be retrieved again, and the file list under Section: CACHE MANIFEST will be checked for updates. The check and update of manifest files and cache files also comply with the browser cache mechanism. If the user manually clears the AppCache cache, the browser will regenerate the cache the next time it loads, which can also be considered a cache update. In addition, Web App can also use code to implement cache updates. Analysis: AppCache seems to be a good caching method. In addition to caching static resource files, it is also suitable for building Web offline apps. In actual use, there are some things that need to be paid attention to, and some of them can be said to be "pitfalls".
In addition, according to official documents, AppCache is no longer recommended and will no longer be supported by the standard. Currently, mainstream browsers still support AppCache, but it is not certain whether this will happen in the future. In the Android embedded Webview, you need to enable AppCache through the Webview settings interface, set the storage path of the cache file, and also set the cache space size.
2.5 Indexed Database IndexedDB is also a database storage mechanism, but it is different from the no longer supported Web SQL Database. IndexedDB is not a traditional relational database and can be classified as a NoSQL database. IndexedDB is similar to the key-value storage method of Dom Storage, but it is more powerful and has a larger storage space. IndexedDB stores data in the form of key-value. Key is required and must be unique; Key can be defined by yourself or automatically generated by the system. Value is also required, but Value is very flexible and can be any type of object. Generally, Value is accessed through Key. IndexedDB provides a set of APIs for storing, retrieving, and traversing data. These APIs are asynchronous, and the results of the operations are returned in callbacks. The following code demonstrates the basic functions of opening (creating) a DB in IndexedDB, creating a storage object (which can be understood as a "table" with relational data), and accessing and traversing data.
Copy the above code to indexed_db.html and open it with Google Chrome browser to add and query data. In Chrome's developer tools, you can view the created DB, storage objects (which can be understood as tables), and the data added to the table. IndexedDB has a very powerful function, which is index. It can generate an index for any attribute in a Value object, and then quickly query the Value object based on the index. To generate an index or support index query data, you need to call the interface to generate the index of the attribute when you generate the storage object. You can create indexes for multiple different attributes of the object at the same time. For example, the following code generates indexes for both the name and email attributes.
After the index is generated, you can query the data based on the index.
Analysis: IndexedDB is a flexible and powerful data storage mechanism that combines the advantages of Dom Storage and Web SQL Database. It is used to store large blocks or complex data, provides larger storage space, and is relatively simple to use. It can be used as a substitute for Web SQL Database. It is not very suitable for caching static files.
Android started to add support for IndexedDB in 4.4. Just turn on the switch to allow JS execution.
#p# 2.6 File System API The File System API is a new storage mechanism added to H5. It provides a virtual file system for Web Apps, just like Native Apps access the local file system. Due to security considerations, this virtual file system has certain limitations. Web Apps can create, read, write, delete, and traverse files (folders) in the virtual file system. The File System API is also an optional caching mechanism, just like the previous SQLDatabase, IndexedDB, and AppCache. The File System API has some specific advantages of its own:
The browser provides two types of storage space for the virtual file system: temporary and persistent. Temporary storage space is automatically allocated by the browser, but may be reclaimed by the browser; persistent storage space requires explicit application, and the browser will give the user a prompt when applying, requiring the user to confirm. Persistent storage space is managed by the WebApp itself, and the browser will not recycle or clear the content. The size of persistent storage space is managed by quotas. When applying for the first time, an initial quota will be allocated, and you need to apply again when the quota is used up. The virtual file system runs in a sandbox. The virtual file systems of different WebApps are isolated from each other, and the virtual file system is also isolated from the local file system. The File System API provides a set of file and folder operation interfaces, with synchronous and asynchronous versions to meet different usage scenarios. The following example demonstrates simple functions and usage through a file creation, reading, and writing example.
Copy the above code into the filesystemmapi.html file and open it with Google Chrome (now only supports the File System API, Chrome 43+, Opera 32+, and Chrome for Android 46+). Since Google Chrome disables the File System API function in local HTML files, add the "—allow-file-access-from-files" command line parameter when starting Chrome. In the screenshot above, the left is the result of HTML running, and the right is the web file system seen in the Chrome developer tools. Basically, the data of several cache mechanisms of H5 can be seen in this developer tool, which is very convenient. Analysis: The File System API brings file system functions to Web Apps, and the functions of Native file system are implemented accordingly in Web Apps. Any scenario where data needs to be managed through files or data management through file systems is more suitable. Up to now, the File System API is not supported by the Android system. 3. Optimization of mobile web loading performance (cache) After analyzing the various caching mechanisms provided by H5, we return to the mobile (for Android, which may also be suitable for iOS). Now Android Apps (including mobile Q and WX) mostly embedded components of Webview (system Webview or QQ browser X5 components), and load some H5 operation activity pages or information pages by embedded Webview. This can give full play to the advantages of the Web front-end: rapid development, release, and flexible up and down. However, Webview also has some problems that cannot be ignored. The more prominent one is that it loads relatively slowly and consumes relatively more traffic. By debugging and catching some H5 pages, it is found that each time an H5 page is loaded, there will be more requests. In addition to the requests of the HTML main URL itself, the JS, CSS, font files, and pictures referenced externally by HTML are all independent HTTP requests, and each request is serialized (maybe there is connection multiplexing). With so many requests being serialized, coupled with the browser's parsing and rendering time, the overall loading time of the web becomes longer; the more requested files, the more traffic it consumes. We can use the above mentioned caching mechanisms to help us optimize the loading performance of the web. Conclusion: Combined with the comparison of various cache mechanisms, static files, such as JS, CSS, fonts, pictures, etc., are suitable for cache through the browser cache mechanism. Caching files can greatly improve the loading speed of the web and save traffic. However, there are some shortcomings: the cached files need to be loaded only after they are loaded; the storage space of the browser cache is limited, and the cache may be cleared; the cached files are not verified. To solve these shortcomings, you can refer to the offline package of QQ, which effectively solves these shortcomings. Data obtained by the Web locally or on the server can be cached through Dom Storage and IndexedDB. It also reduces interaction with Server to a certain extent, improves loading speed, and saves traffic. Of course, the performance optimization of the web also includes choosing the appropriate image size to avoid blockage caused by JS and CSS, etc. This requires colleagues on the front-end of the web to optimize according to some specifications and some debugging tools. For more information, please follow our WeChat ID: weixinBugly or search for our WeChat ID: weixinBugly. Tencent Bugly Bugly is an outsourced version of Tencent's internal product quality monitoring platform. It supports two mainstream platforms: iOS and Android. Its main function is to monitor and report crashes and stutters on the user side after the app is released, so that developers can understand the quality of the app for a short time and modify them in time. Currently, all products within Tencent are using them to monitor the crash of online products. Tencent’s internal team has been polished for 4 years. Currently, all Tencent’s internal products are in use, basically covering the mobile devices and network environment in the Chinese market, and the reliability is guaranteed. Using Bugly, you have used the same quality as mobile QQ, QQ space, and mobile phone manager. |
<<: A review of the nine major events in the Internet industry in 2015
>>: Android 6.1 first revealed: two major features are coming!
In order for an activity to be successful, adequa...
Huawei can rely on its existing ARMv8 licenses fo...
When in Rome, do as the Romans do is a tradition ...
A few days ago, when I was surfing the Internet, ...
Audit expert: Wang Guoyi Postdoctoral fellow in N...
From wild boars in Nanjing to weasels in Beijing,...
Table of contents: 1. Contact information of iOS ...
After the emergence of WeChat mini-programs, soci...
Counting the days, from graduation to now, I have...
On September 1, the world's leading academic ...
Server memory is also memory (RAM). There is no o...
Today I will share with you a small online entrep...
The sudden outbreak of the new coronavirus epidem...
With the rapid development of the Internet era, w...
My app development project this summer is Instant...