@sodiumlabs/cache

Discord server npm version npm downloads Last commit

About

A lightweight and performant caching library providing simple TTL-based caches for single values and key-value maps.

Installation

Node.js 18 or newer is required.

npm install @sodiumlabs/cache

Usage

Cache

import { Cache } from "@sodiumlabs/cache";

// values are cached for 60s:
const cache = new Cache({ ttl: 60_000 });

cache.set("some key", 10);

cache.get("some key"); // 10
cache.has("some key"); // true
cache.set("some key", 10, 10_000); // override the default TTL with 10s

await sleep(11_000);

cache.has("some key"); // false

ValueCache

import { ValueCache } from "@sodiumlabs/cache";

const value = new ValueCache({ ttl: 10_000 }).set("some value");

value.get(); // "some value"

await sleep(11_000);

value.get(); // undefined

Links

Help

Need help with the module? Ask on our support server!