Skip to content

Commit 2ce0e07

Browse files
committed
v2.0.0
1 parent 8932735 commit 2ce0e07

10 files changed

+232
-889
lines changed

Gruntfile.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

README.md

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,110 @@ Cache API is currently available in Chrome >= 40, Firefox >=39 and Opera >= 27.
88

99
Safari and Edge recently introduced support for it.
1010

11-
# Install
11+
Info by Google: https://developers.google.com/web/fundamentals/instant-and-offline/web-storage/cache-api
1212

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
1323
```
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));
1535
```
1636

17-
# Use
37+
### get:
1838

19-
```html
20-
<script src="/cache-api-keyval-full.js"></script>
21-
<script>
39+
```js
40+
import { get } from 'cache-api-keyval';
2241

23-
// load database
24-
var db = new CacheApiDB('my-store', { namespace: 'optional' });
42+
// logs: "world"
43+
get('hello').then(val => console.log(val));
44+
```
2545

26-
// set JSON object data
27-
db.set('key', { json: 'object' });
46+
If there is no 'hello' key, then `val` will be `undefined`.
2847

29-
// set text data with expiration in 24 hours
30-
db.set('key2', 'string', 86400);
48+
### keys:
3149

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';
3652

37-
// delete key from database
38-
db.del('key2');
53+
// logs: ["hello", "foo"]
54+
keys().then(keys => console.log(keys));
55+
```
3956

40-
// prune expired cache entries
41-
db.prune();
57+
### del:
4258

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';
4561

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';
4969

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:
5474

55-
/* initiate database with certain availability */
75+
```js
76+
import { prune } from 'cache-api-keyval';
5677

57-
});
58-
</script>
78+
prune();
5979
```
6080

61-
## Fallback storage
81+
The prune method clears all expired keys.
82+
83+
### Custom stores:
6284

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:
6486

6587
```js
66-
window.CacheApiDBFallback = function(store, options) {
67-
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');
7292
```
7393

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`.
7597

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.
7798

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`.
99+
## Installing
79100

101+
### Via npm + webpack/rollup
80102

81-
```html
82-
<script src="/cache-api-keyval-no-fallback-expire.js"></script>
83-
<script>
103+
```sh
104+
npm install --save cache-api-keyval
105+
```
84106

85-
// check if Cache API is available
86-
if ("caches" in window) {
107+
Now you can require/import `cache-api-keyval`:
108+
109+
```js
110+
import { get, set } from 'cache-api-keyval';
111+
```
87112

88-
// load database
89-
var db = new CacheApiDB('my-store', { namespace: 'optional' });
113+
### Via `<script>`
90114

91-
// ...
92-
}
115+
* `dist/cache-api-keyval.mjs` is a valid JS module.
116+
* `dist/cache-api-keyval-iife.js` can be used in browsers that don't support modules. `CacheApiDB` is created as a global.
117+
* `dist/cache-api-keyval-iife.min.js` As above, but minified.

cache-api-keyval.ext.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)