Talking about behaviors objects in the Polymer framework of JavaScript, polymerbehaviors

Source: Internet
Author: User

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.

Related Article

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.