Talking about behaviors objects in the Polymer framework of JavaScript, polymerbehaviors
Should localStorage be a household name? But the local storage family can be far more than it. I used sessionStorage before, and now there is a magic CacheStorage. It is used to store Response objects. That is to say, it is used to Cache HTTP and response. Although localStorage can also be used, it may be more professional.
The reference name of CacheStorage in the browser is caches instead of the cacheStorage written in the camper. It is defined in the ServiceWorker specification. CacheStorage is a collection of multiple caches, and each Cache can store multiple Response objects.
No more nonsense. The following is a 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 call the open Method on caches to obtain a Cache object reference asynchronously. On this object, we can put the Response object (the parameter is a URL and a Response object) and retrieve it using the match method (pass in a URL to retrieve the corresponding Response object ).
The match method can be used not only to call CacheStorage on the Cache, but also to use the match method. For example, two caches are opened in the preceding example, and a URL named/hehe is written. After the write operation is complete, call the match method on the external CacheStorage to match/hehe. The result is random (no rule is defined ).
Although only one data is put for the Cache object in the above example, the Cache object itself can store more URL/Response pairs. It also provides methods such as delete (User deletion) and keys (for traversal. However, the Cache does not have the clear method like localStorage. If you want to clear a Cache, you can directly delete the entire Cache on CacheStorage and open it again.
This set of APIs and ServiceWorker are usually used in ServiceWorker, and the entire design style is also based on Promise like ServiceWorker.