Skip to content
Storage Cache for Web Extensions
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
README.md update name Jun 7, 2019
index.d.ts update name Jun 7, 2019
index.js
license
package.json
yarn.lock

README.md

webext-storage-cache

Externalized from https://github.com/sindresorhus/refined-github/pull/2109

Install

$ yarn add webext-storage-cache

Requirements

This module requires webextension-polyfill and the storage permission:

# manifest.json
{
  "permissions": [
    "storage"
  ],
  "background": {
    "scripts": [
      "browser-polyfill.min.js"
    ],
  },
  "content_scripts": [
    {
      "js": [
        "browser-polyfill.min.js"
      ]
    }
  ]
}

Usage

import cache from 'webext-storage-cache';

(async () => {
  let cachableItem = await cache.get('unique');
  if (cacheableItem === undefined) {
    cachableItem = await someFunction();
    await cache.set('unique', cachableItem, 3 /* days */);
  }
  console.log(cachableItem);
})();

API

All methods a return a Promise

cache.get(key)

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

cache.has(key)

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

cache.set(key, value, expiration)

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

You can’t perform that action at this time.