Basic knowledge of behaviors object in the polymer frame of javascript

Source: Internet
Author: User

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.

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.