A brief discussion on behaviors objects in JavaScript's Polymer framework
Should localStorage be a household name? But the local storage family is much more than it. I used to talk about sessionStorage, but now there is a magical CacheStorage. It is used to store Response objects. That is to say, it is used to cache HTTP and responses. Although localStorage can also be done, it may be more professional.
The reference to CacheStorage on the browser is caches instead of cacheStorage written in the camel, which is defined in the ServiceWorker specification. CacheStorage is a collection of multiple caches, and each cache can store multiple Response objects.
Can't say much nonsense, here's the demo
<script> ('c1'); ('c2'); ([ ('c1').then(function(cache) { return ('/hehe', new Response('aaa', { status: 200 })); }), ('c2').then(function(cache) { return ('/hehe', new Response('bbb', { status: 200 })); }) ]).then(function() { return ('/hehe'); }).then(function(response) { return (); }).then(function(body) { (body); }); </script>
First, calling the open method on caches can get a reference to a Cache object asynchronously. On this object, we can put the Response object in (the parameters are a URL and a Response object), and use the match method to take it out (pass a URL and take out the corresponding Response object).
The match method can not only call CacheStorage on Cache. There are also match methods on CacheStorage. For example, the above example opens two caches, both of which write a URL called /hehe. After the write operation is completed, the match method is called on the external CacheStorage to match /hehe, and the result is random (there is nowhere to be found for this rule).
Although the above example only puts one data on the Cache object, the Cache object itself can store more URL/Response pairs. And provide methods such as delete (user deletion), keys (for traversal). However, Cache does not have a clear method like localStorage. If you have to clear a cache, you can delete the entire cache directly on CacheStorage and then open it again.
This API is a serviceWorker, which is usually used in ServiceWorker, and the entire design style is based on Promise like ServiceWorker.
Related Articles
The difference between Javascript typeof and instanceof
In JavaScript, typeof and instanceof are often used to determine whether a variable is empty or what type it is. But there are still differences between them. Friends who need it can refer to it.2016-10-10Getting started with JavaScript JavaScript has a full range of operators
Getting started with JavaScript JavaScript has a full range of operators...2007-10-10The difference between jquery and javascript (comparison of common methods)
jquery is an extension of javascript, encapsulation, which is to make javascript more useful and simpler. In order to illustrate the difference, let’s share with you the common methods of JavaScript and JQuery.2013-07-07Detailed explanation of the use of Promise in JavaScript
This article mainly introduces the detailed explanation of the use of Promise in JavaScript. The promise object is an important knowledge point in advanced JS learning. Friends who need it can refer to it.2015-06-06Tips for creating two-dimensional arrays in JavaScript
A two-dimensional array is essentially an array with an array as an array element, that is, "array of array", type specifier array name [constant expression] [constant expression]. Two-dimensional arrays are also called matrices, and matrices with equal rows and columns are called square matrixes. Symmetric matrix a[i][j] = a[j][i], diagonal matrix: n-order square matrix has zero elements outside the diagonal line2021-11-11JS code to determine whether the page is closed or refreshed
JS code to determine whether the page is closed or refreshed...2007-01-01JavaScript implementation example to store hmtl strings
This article mainly introduces the example of javascript implementing storing hmtl strings. Friends who need it can refer to it2014-04-04Example of image cutting multi-block effect implemented by javascript
This article mainly introduces the multiple effects of image cutting in JavaScript, involving javascript's skills in operating pictures and css styles. Friends who need it can refer to it2015-05-05Sharing of mailto usage tips
How to use mailto? It is not unfamiliar to most people, but have you noticed the practical details? Let’s share my experience during use. Friends who need to know can refer to it.2012-12-12How to create an XMLHttpRequest object with ajax
How to create ajax proxy object? I know how to do it~ I just don't know how to express it in language? This article will introduce in detail how to solve this problem2012-12-12