npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@suckless/cache

v0.6.0

Published

Minimal, type-safe cache with pluggable adapters

Readme

@suckless/cache

Minimal, type-safe cache with pluggable adapters. Ships with an in-memory adapter. ~120 lines, zero dependencies.

Install

npm install @suckless/cache

Usage

import { createCache, memoryAdapter } from "@suckless/cache"

const cache = createCache(memoryAdapter<string, number>())

await cache.set("counter", 1)
await cache.get("counter") // 1
await cache.delete("counter")
await cache.get("counter") // undefined

TTL

Pass a TTL in milliseconds per entry or as a default for the entire cache:

// Per-entry TTL
await cache.set("key", "value", 5000) // expires in 5s

// Default TTL for all entries
const cache = createCache(memoryAdapter<string, string>(), 5000)
await cache.set("key", "value") // uses default 5s TTL
await cache.set("key", "value", 1000) // overrides with 1s TTL

Fetch

fetch combines a cache lookup with a fallback. On a miss, it calls the fetcher, caches the result, and returns it. Concurrent calls for the same key are deduplicated — the fetcher runs once and all callers receive the same result.

const user = await cache.fetch(
	"user:42",
	async () => {
		const res = await fetch("https://api.example.com/users/42")
		return res.json()
	},
	60_000,
)

Teardown

Both the cache and adapters implement AsyncDisposable. Use await using for automatic cleanup, or call [Symbol.asyncDispose]() directly:

{
	await using cache = createCache(memoryAdapter<string, string>())
	await cache.set("key", "value")
	// cache is disposed when scope exits
}

// or manually
const cache = createCache(memoryAdapter<string, string>())
// ...
await cache[Symbol.asyncDispose]()

For the built-in memory adapter, dispose clears the store and stops the background sweep timer. For custom adapters, dispose is where you close connections or release resources.

Custom Adapters

An adapter is any object that implements CacheAdapter:

import type { CacheAdapter } from "@suckless/cache"

interface CacheAdapter<K extends string, V> extends AsyncDisposable {
	get(key: K): Promise<V | undefined>
	set(key: K, value: V, ttl?: number): Promise<void>
	delete(key: K): Promise<void>
}

The adapter is responsible for TTL enforcement. If your backend handles expiration natively (Redis, Postgres), pass the ttl through. If not, implement it yourself.

Redis adapter example (Bun)

import { createClient } from "bun"
import { createCache, type CacheAdapter } from "@suckless/cache"

function redisAdapter<K extends string, V>(url: string): CacheAdapter<K, V> {
	const client = createClient(url)

	return {
		async get(key) {
			const raw = await client.get(key)
			if (raw === null) return undefined
			return JSON.parse(raw) as V
		},

		async set(key, value, ttl) {
			const serialized = JSON.stringify(value)
			if (ttl !== undefined) {
				await client.set(key, serialized, { PX: ttl })
			} else {
				await client.set(key, serialized)
			}
		},

		async delete(key) {
			await client.del(key)
		},

		async [Symbol.asyncDispose]() {
			await client.close()
		},
	}
}

const cache = createCache(redisAdapter("redis://localhost:6379"))

Postgres adapter example (Bun)

import { SQL } from "bun"
import { createCache, type CacheAdapter } from "@suckless/cache"

function postgresAdapter<K extends string, V>(url: string): CacheAdapter<K, V> {
	const sql = new SQL(url)

	return {
		async get(key) {
			const [row] = await sql`
        SELECT value FROM cache
        WHERE key = ${key}
        AND (expires_at IS NULL OR expires_at > NOW())
      `
			if (row === undefined) return undefined
			return row.value as V
		},

		async set(key, value, ttl) {
			const expiresAt = ttl !== undefined ? new Date(Date.now() + ttl) : null

			await sql`
        INSERT INTO cache (key, value, expires_at)
        VALUES (${key}, ${JSON.stringify(value)}, ${expiresAt})
        ON CONFLICT (key) DO UPDATE
        SET value = EXCLUDED.value, expires_at = EXCLUDED.expires_at
      `
		},

		async delete(key) {
			await sql`DELETE FROM cache WHERE key = ${key}`
		},

		async [Symbol.asyncDispose]() {
			await sql.close()
		},
	}
}

const cache = createCache(postgresAdapter("postgres://localhost:5432/mydb"))

API

createCache<K, V>(adapter, defaultTtl?)

Creates a cache instance.

  • adapter — a CacheAdapter<K, V>.
  • defaultTtl — default TTL in milliseconds. Applied when set or fetch is called without an explicit TTL.

Returns a Cache<K, V>.

memoryAdapter<K, V>(sweepIntervalMs?)

Creates an in-memory CacheAdapter.

  • sweepIntervalMs — interval for background expiry cleanup. Defaults to 30,000 (30s).

Cache<K, V>

  • get(key) — returns the cached value or undefined
  • set(key, value, ttl?) — stores a value with optional TTL in ms
  • delete(key) — removes a value
  • fetch(key, fetcher, ttl?) — get-or-set with deduplication
  • [Symbol.asyncDispose]() — teardown

License

MIT