JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1155
  • Score
    100M100P100Q105744F
  • License ISC

A better in-memory cache

Package Exports

  • keshi

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 (keshi) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Keshi

Keshi on NPM Keshi on TravisCI

Keshi is a better in-memory (or custom) cache for Node and the browser.

const createCache = require('keshi');

or

import createCache from 'keshi';

Usage

const cache = createCache();

const user = await cache.resolve('user', () => fetch('https://myapi.com/user').then(r => json()), '30 mins');

What this will do:

  • Fetch the user from the API as it doesn't have it in cache.
  • If called again within 30 minutes it will return the cached user.
  • If called after 30 minutes it will fetch the user again and re-cache.

Cache the data you need

You should return only the data you need to keep the cache efficient. Here's a real world example of caching repository information from GitHub:

// In the browser
const fetchProjectMeta = (user, repo) =>
  fetch(`https://api.github.com/repos/${user}/${repo}`)
    .then(r => r.json())
    .then(r => ({ name: r.full_name, description: r.description }));

// ...or in Node
const fetchProjectMeta = (user, repo) =>
  got
    .get(`https://api.github.com/repos/${user}/${repo}`, { json: true })
    .then(r => ({ name: r.body.full_name, description: r.body.description }));

// And call it (for 1 hour it will return cached results).
const meta = await cache.resolve('myRepo', fetchProjectMeta('DominicTobias', 'keshi'), '1 hour');

Rate limited APIs (as above), saving bandwidth, dealing with poor client network speeds, returning server responses faster are some of the reasons you might consider caching requests.

Keshi will automatically keep memory low by cleaning up expired items.

API

resolve(key, [value], [expiresIn])

key → String → Required

value → Any → Optional

A function which resolves to a value, or simply a literal value.

expiresIn → Number | String → Optional

A number in milliseconds or anything that ms accepts after which the value is considered expired. If no expiry is provided the item will never expire.

del(key, matchStart)

Delete a cached item by key.

You can also delete any that start with the key by passing true to matchStart.

cache.del('project.5c4a351f8f49cf1097394204.', true)

clear()

Clear all cached items.

Custom storage

The default cache is in-memory, however the storage can be anything you like. To pass in a custom storage:

const cache = createCache({ customStorage });

Your cache must implement the following methods:

customStorage.get(key)

Returns the cache value given the key. Cache values must be returned as an Array of [value, <expiresIn>]. expiresIn is an ISO Date string.

This method can be async.

customStorage.set(key, value)

Values set are of type Array in the following format: [value, <expiresIn>]. expiresIn should be an ISO Date string.

This method can be async.

customStorage.del(key)

Removes the item specified by key from the cache.

This method can be async.

customStorage.keys()

Returns an array of cache keys.

customStorage.clear()

Clears all items from the cache.

Example

import { get, set, keys, del, clear } from 'idb-keyval';

const customStorage = { get, set, keys, del, clear };
const cache = createCache({ customStorage });