Analysis of off-line storage (Offline WEB applications)

Source: Internet
Author: User
Tags unique id
Analysis of off-line storage (Offline Web applications)

1. Overview

This feature is a new feature of HTML5 that enables Web programs to specify that local resources can be slow to exist, and caching policies that enable programs to run offline, or to load resources locally while the program is online without having to reload from the network. In WebKit, this feature is controlled by Offline_web_applications macros.

2. Content of the agreement

Add the Manifest property to the HTML tag in the HTML file to specify the manifest file that the Web application needs to use, and the Web application author can specify a list of files to cache in the file.

[Note: Manifest tags inserted in HTML files]

[Note: Manifest file Example]

The manifest file is a cached list that tells the browser what needs to be cached in the client's file. Manifest is a plain text file mimetype as text/cache-manifest.

Network, CACHE, fallback are optional.

MANIFEST file must begin with CACHE MANIFEST

Subsequent files that need to be downloaded and cached use a relative or absolute path, with each resource row.

The network segment indicates the domain in which some online resources are located, all resources under this domain are not cached, and even if the client is offline, attempts are being taken to request resources based on those domains.

The cache segment is not clear. Each line below the fallback section contains two parts, separated by a space. The second half is an alternative path when the current partial path is inaccessible


3. The offline-web-application in WebKit

The documents relating to Offline-web-application in WebKit are:

Applicationcache. [H/cpp]
Applicationcachegroup. [H/cpp]
Applicationcachehost. [H/cpp]
ApplicationCacheHostInternal.h
Applicationcacheresource. [H/cpp]
Applicationcachestorage. [H/cpp]

3.1 The relationships of several classes are as shown:

[Note: Diagram of several classes]

Applicationcachehost's job is to target WebKit interfaces, which are responsible for obtaining the appropriate resources (if any) from the cache subsystem when WebKit loads the resource.

Applicationcachegroup is directly related to manifest, and a applicationcachegroup serves only one manifest. Each applicationcachegroup has one or more versions of Applicationcache, one of which has a applicationcache as the "latest" Applicationcache.

Applicationcache represents a version of a manifest cache.

Applicationcacheresource represents a resource in the manifest.

Applicationcachestorage can record Applicationcachegroup, Applicationcache, Applicationcacheresource, each record has a unique ID, in these three classes, There is also a corresponding representation (Storage-id).

3.2 Another angle to understand the relationship between several classes:

You can understand the relationship between the Applicationcachegroup, Applicationcache, and Applicationcacheresource three classes in the database by looking at how they are stored.

[Note: How several classes are stored in the database]

3.3 A relatively simple manifest processing process:

[Note: The process of offline storage creation]

The diagram above depicts a simplified version of the process from parsing to HTML files containing manifest tags to the associated buffering process. 3.4 A more complete description: 3.4.1 Cache build, Update:

When you start loading and creating Documentloader, create a applicationcachehost and associate it with Documentloader.

When Mainresourceloader calls load, it first checks the offline-web-by the Applicationcachehost::maybeloadmainresource call Application (cache) If there is a primary resource document caching, there, then load from the cache, otherwise load from the network.

Loads the master resource and resolves it, and when parsing to the manifest tag, the Applicationcachegroup object that corresponds to the manifest-url is obtained by manifest (new) If there is one in the database that is read from the database.

Initiates the manifest download process, if 304 (not Modified) is returned or the locally saved manifest file is exactly the same as the downloaded manifest file, the Applicationcachegroup is completed To Noupdate and exit the process, otherwise the following process is performed.

After the download is complete, parse the manifest file and analyze each entry (one entry in each entry representative manifest file).

Initiate the update process. Call Applicationcachegroup::startloadingentry () to download each entry, and if a entry is returned with an HTTP error of 404 or 410, stop update. Place the Applicationcachegroup state as ' failure ' and exit the process.

When all entry are loaded successfully, the status is complete and the loaded Applicationcache is set to the latest Applicationcache in Applicationcachegroup, Associate this latest Applicationcache with the current applicationcachehost. and update the database through Applicationcachestorage.

Use of the 3.4.2 cache

When Mainresourceloader or Resourceloader loads a resource, it checks to see if the resource has been cached, if it has been cached, and the current Applicationcachehost The associated Applicationcache is not empty and is a complete state, the corresponding resource data is taken from the cache.

4. The difference from local storage (native Storage)

As mentioned above, offline storage is a complete resource caching system, including JAVASCRIPT,CSS, picture resources, and even master resources.

Local storage is actually just a local hash table (Key/value pair), which is usually used by JS to hold strings. The entire browser has only such a hash table.

5. Summary

The off-line storage is widely used in the current HTML5 game, which greatly improves the loading speed. And considering the future of software web, off-line storage can have more general application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.