@dnode/cache

[![dependencies | 0 | 0](https://img.shields.io/badge/dependencies-0%20|%200-green.svg)](DEPENDENCIES.md)

Stats

StarsIssuesVersionUpdatedCreatedSize
@dnode/cache
004.0.13 years ago4 years agoMinified + gzip package size for @dnode/cache in KB

Readme

dependencies | 0 | 0

Usage

const cache = require('@dnode/cache')(client);

const value = await cache(
  'key',
  async () => 'value'
);

If the client supports setJSON() and getJSON() (e.g. @dnode/redis) it will be used to encode/decode the values.

const cache = require('@dnode/cache')(client);

const value = await cache(
  'key',
  async () => ({ example: 'example' })
);

Expire

An expire (in seconds) can be passed to automatically remove the value from the cache after certain time.

const cache = require('@dnode/cache')(client);

const value = await cache(
  'key',
  async () => 'value',
  1 * 60 * 60
);

If the expire is an object supporting asSeconds() (e.g. @dnode/duration or moment.duration) it will be used.

const cache = require('@dnode/cache')(client);
const duration = require('@dnode/duration');

const value = await cache(
  'key',
  async () => 'value',
  duration('1h')
);

Invalidate

The invalidate will be called after getting the value from the cache and can, if return true, force to get a new value.

const cache = require('@dnode/cache')(client);

const value = await cache(
  'key',
  async () => 'value',
  { invalidate: value => value.invalid }
);

Refresh

If refresh is true, the expire will always be refreshed if the key is available and the value is valid.

const cache = require('@dnode/cache')(client);

const value = await cache(
  'key',
  async () => 'value',
  { refresh: true }
);

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.