Cache API Key/Value Store
Fast and tiny key/value store with +50MB storage capacity in most browsers, expiration and JSON object data-type support.
Cache API is currently available in Chrome >= 40, Firefox >=39 and Opera >= 27.
Safari and Edge recently introduced support for it.
// set JSON object dataCacheApiDB;// set text data with expiration in 24 hoursCacheApiDB;// get data from cacheCacheApiDB;// delete key from databaseCacheApiDB;// clear databaseCacheApiDBclear;// prune expired cache entriesCacheApiDB;
;;; // expire in 1 hour
The data is stored in JSON format and supports big data.
All methods return promises:
;// logs: "world";
If there is no 'hello' key, then
val will be
;// logs: ["hello", "foo"];
The prune method clears all expired keys.
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:
With thanks to idb-keyval. The
v2.0.0 code structure has been copied from
Via npm + webpack/rollup
npm install --save cache-api-keyval
Now you can require/import
dist/cache-api-keyval.mjsis a valid JS module.
dist/cache-api-keyval-iife.jscan be used in browsers that don't support modules.
CacheApiDBis created as a global.
dist/cache-api-keyval-iife.min.jsAs above, but minified.