Skip to content

mish-tv/cache

Repository files navigation

@mish-tv/cache

npm Build and test coverage license

`@mish-tv/cache` is a simple cache library that holds a single value in a single instance.

Installation

npm install --save @mish-tv/cache

Simple Usage

import { Cache } from "@mish-tv/cache";

// By default, it caches for 1hour + rand()*6min.
const entityCache = new Cache<Entity>();

// Only at the beginning and when it expires,
// it calls an anonymous function passed as an argument and
// caches the returned value.
const getEntity = () =>
  entityCache.get(async () => {
    const entity: Entity = await fetch();

    return entity;
  });

(async () => {
  const entity = await getEntity();
})();

Other Usage

// You can configure ttl, jitter and initial value.
const entityCache = new Cache(60000, 1000, entity);

// You can create a cache that will never expire.
const entityCache = new Cache("infinity");