Skip to content

cache

Minified4.14 KBMinzipped1.21 KB

Functional cache utilities.

Example

ts
import { Cache } from "@monstermann/cache";

// Create a LRU cache with a maximum of 100 items
const cache = Cache.LRU<string, number>({ max: 100 });

// Set values
Cache.set(cache, "foo", 1);
Cache.set(cache, "bar", 2);

// Get values
const foo = Cache.get(cache, "foo"); // 1
const missing = Cache.get(cache, "unknown"); // undefined

// Get or compute and cache a value
const score = Cache.getOrElse(cache, "baz", (c) => {
    return Cache.set(c, "baz", 3);
});

// Check existence
Cache.has(cache, "foo"); // true

// Remove values
Cache.remove(cache, "bar");

Installation

sh
npm install @monstermann/cache
sh
pnpm add @monstermann/cache
sh
yarn add @monstermann/cache
sh
bun add @monstermann/cache

Tree-shaking

Installation

sh
npm install -D @monstermann/unplugin-cache
sh
pnpm -D add @monstermann/unplugin-cache
sh
yarn -D add @monstermann/unplugin-cache
sh
bun -D add @monstermann/unplugin-cache

Usage

ts
// vite.config.ts
import cache from "@monstermann/unplugin-cache/vite";

export default defineConfig({
    plugins: [cache()],
});
ts
// rollup.config.js
import cache from "@monstermann/unplugin-cache/rollup";

export default {
    plugins: [cache()],
};
ts
// rolldown.config.js
import cache from "@monstermann/unplugin-cache/rolldown";

export default {
    plugins: [cache()],
};
ts
// webpack.config.js
const cache = require("@monstermann/unplugin-cache/webpack");

module.exports = {
    plugins: [cache()],
};
ts
// rspack.config.js
const cache = require("@monstermann/unplugin-cache/rspack");

module.exports = {
    plugins: [cache()],
};
ts
// esbuild.config.js
import { build } from "esbuild";
import cache from "@monstermann/unplugin-cache/esbuild";

build({
    plugins: [cache()],
});