JSPM

  • Created
  • Published
  • Downloads 6082
  • Score
    100M100P100Q132523F
  • License MIT

WebExtensions module: Map-like promised cache storage with expiration. Chrome and Firefox.

Package Exports

  • webext-storage-cache

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (webext-storage-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

webext-storage-cache

WebExtensions module: Map-like promised cache storage with expiration. Chrome and Firefox.

Travis build status npm version

This module works on content scripts, background pages and option pages.

Install

You can just download the standalone bundle (it might take a minute to download) and include the file in your manifest.json, or:

npm install --save webext-storage-cache
import storageCache from 'webext-storage-cache';
const storageCache = require('webext-storage-cache');

Usage

This module requires the storage permission:

// manifest.json
{
  "permissions": [
    "storage"
  ]
}
import cache from 'webext-storage-cache';

(async () => {
  if (!await cache.has('unique')) {
    const cachableItem = await someFunction();
    await cache.set('unique', cachableItem, 3 /* days */);
    }

  console.log(await cache.get('unique'));
})();

API

Similar to a Map(), but all methods a return a Promise.

cache.has(key)

Checks if the given key is in the cache, returns a boolean.

key

Type: string

cache.get(key)

Returns the cached value of key if it exists and hasn't expired, returns undefined otherwise.

key

Type: string

cache.set(key, value, expiration /* in days */)

Caches the given key and value for a given amount of days.

key

Type: string

value

Type: string | number | boolean or array | object of those three types

expiration

The number of days after which the cache item will expire.

Type: number Default: 30

cache.delete(key)

Deletes the requested item from the cache.

key

Type: string

License

MIT © Federico Brigante — Twitter, Connor Love