@maxdome/cache

1.0.2 • Public • Published

Usage

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

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

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

const cache = require('@maxdome/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('@maxdome/cache')(client);

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

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

const cache = require('@maxdome/cache')(client);
const duration = require('@maxdome/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('@maxdome/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('@maxdome/cache')(client);

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

Readme

Keywords

none

Package Sidebar

Install

npm i @maxdome/cache

Weekly Downloads

1

Version

1.0.2

License

none

Unpacked Size

3.84 kB

Total Files

4

Last publish

Collaborators

  • dragonnodejs
  • sharaal
  • fabsrc
  • markusz
  • mxdbv
  • csaba.lorincz
  • goexrock
  • dimified