Localstorage should be a household name? But local storage is much more than that. Before Sessionstorage, and now there is a magical cachestorage. It is used to store the Response object. In other words, it is used to cache HTTP, response. Although Localstorage can do it, it may be more professional.
Cachestorage's reference in the browser is called caches rather than the cachestorage of the hump, which is defined in the Serviceworker specification. Cachestorage is a collection of multiple cache, and each cache can store multiple Response objects.
No more nonsense, here is demo
<script>
caches.delete (' C1 ');
Caches.delete (' C2 ');
Promise.all ([
caches.open (' C1 '). Then (function (cache) {return
cache.put ('/hehe ', New Response (' AAA '), { STATUS:200}))
,
caches.open (' C2 '). Then (function (cache) {return cache.put ('
/hehe '), New Response (' BBB ', {status:200})])
. Then (function () {return
caches.match ('/hehe ');
}). Then (function (response) {return
response.text ();
}). Then (function (body) {
console.log (body);
});
</script>
First, you can get a reference to a Cache object asynchronously by using the open method on caches. On this object we can put the Response object in (the parameter is a URL and a Response object), take out with the match method (pass in a URL to take out the corresponding Response object). The
Match method can be used not only on cache cachestorage but also on the match method, for example, the above example opens up two Cache and writes a URL called/hehe. After the write operation is complete, the cachestorage to the outside is adjusted to match the/hehe with the match method, and the result is random (where the rule is not found).
Although the above example only put a data on the cache object, the cache object itself can store more url/response pairs. It also provides methods such as delete (user deletion), keys (for traversal), and so on. But the cache is not like Localstorage have clear method, if you have to empty a cache, you can directly on the cachestorage the entire Cache to delete and then open again.
This set of APIs and the Serviceworker, which is commonly used in Serviceworker, is based on Promise as well as the entire design style.