You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+77-52Lines changed: 77 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,85 +8,110 @@ Cache API is currently available in Chrome >= 40, Firefox >=39 and Opera >= 27.
8
8
9
9
Safari and Edge recently introduced support for it.
10
10
11
-
# Install
11
+
Info by Google: https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api
12
12
13
+
## Usage
14
+
15
+
16
+
### set:
17
+
18
+
```js
19
+
import { set } from'cache-api-keyval';
20
+
21
+
set('hello', 'world');
22
+
set('foo', 'bar', 3600); // expire in 1 hour
13
23
```
14
-
npm install --save cache-api-keyval
24
+
25
+
The data is stored in JSON format and supports big data.
26
+
27
+
All methods return promises:
28
+
29
+
```js
30
+
import { set } from'cache-api-keyval';
31
+
32
+
set('hello', 'world')
33
+
.then(() =>console.log('It worked!'))
34
+
.catch(err=>console.log('It failed!', err));
15
35
```
16
36
17
-
#Use
37
+
### get:
18
38
19
-
```html
20
-
<scriptsrc="/cache-api-keyval-full.js"></script>
21
-
<script>
39
+
```js
40
+
import { get } from'cache-api-keyval';
22
41
23
-
// load database
24
-
var db =newCacheApiDB('my-store', { namespace:'optional' });
42
+
// logs: "world"
43
+
get('hello').then(val=>console.log(val));
44
+
```
25
45
26
-
// set JSON object data
27
-
db.set('key', { json:'object' });
46
+
If there is no 'hello' key, then `val` will be `undefined`.
28
47
29
-
// set text data with expiration in 24 hours
30
-
db.set('key2', 'string', 86400);
48
+
### keys:
31
49
32
-
// get data from cache
33
-
db.get('key').then(function(json) {
34
-
console.log('json object', json);
35
-
});
50
+
```js
51
+
import { keys } from'cache-api-keyval';
36
52
37
-
// delete key from database
38
-
db.del('key2');
53
+
// logs: ["hello", "foo"]
54
+
keys().then(keys=>console.log(keys));
55
+
```
39
56
40
-
// prune expired cache entries
41
-
db.prune();
57
+
### del:
42
58
43
-
// if no fallback is provided, all methods will reject and the constructor will contain `no` with integer 1.
44
-
if (db.no) {
59
+
```js
60
+
import { del } from'cache-api-keyval';
45
61
46
-
// Cache API is not supported by the browser
47
-
48
-
}
62
+
del('hello');
63
+
```
64
+
65
+
### clear:
66
+
67
+
```js
68
+
import { clear } from'cache-api-keyval';
49
69
50
-
// optional: wait for async browser check to complete
51
-
// the test checks if Cache API is blocked by privacy or cookie settings
52
-
// when blocked, the fallback storage is loaded
53
-
onCacheApiDB(function() {
70
+
clear();
71
+
```
72
+
73
+
### prune:
54
74
55
-
/* initiate database with certain availability */
75
+
```js
76
+
import { prune } from'cache-api-keyval';
56
77
57
-
});
58
-
</script>
78
+
prune();
59
79
```
60
80
61
-
## Fallback storage
81
+
The prune method clears all expired keys.
82
+
83
+
### Custom stores:
62
84
63
-
To enable a fallback storage for browsers that do not yet support Cache API, you can define a constructor on global variable `CacheApiDBFallback`. The constructor needs to provide 4 methods:`set`, `get`, `del` and `prune`.
85
+
By default, the methods above use a default `keyval` store. You can create your own store, and pass it as an additional parameter to any of the above methods:
this.get=function(key) { /* return key from store */ }
68
-
this.set=function(key,data,expire) { /* set key in store */ }
69
-
this.del=function(key) { /* delete key from store */ }
70
-
this.prune=function() { /* cleanup database */ }
71
-
};
88
+
import { get, set } from'cache-api-keyval';
89
+
90
+
set('foo', 'bar', 'custom-store');
91
+
get('foo', 'custom-store');
72
92
```
73
93
74
-
## Tinier
94
+
That's it!
95
+
96
+
With thanks to [idb-keyval](https://github.com/jakearchibald/idb-keyval). The `v2.0.0` code structure has been copied from `idb-keyval`.
75
97
76
-
The complete library provides a fallback mechanism, a check to detect if Cache API is blocked by browser privacy settings and cache expire functionality. While at `871 bytes` compressed it is small, some users may prefer just the `key/val` part with optional expire functionality.
77
98
78
-
[cache-api-keyval-no-fallback.js](https://github.com/optimalisatie/Cache-API-Key-Value-Store/blob/master/src/cache-api-keyval-no-fallback.js) is a stripped version without error reporting and fallback mechanism with a size of `688 bytes`. [cache-api-keyval-no-fallback-expire.js](https://github.com/optimalisatie/Cache-API-Key-Value-Store/blob/master/src/cache-api-keyval-no-fallback-expire.js) is further stripped of expire functionality for a compressed size of `461 bytes`.
0 commit comments