Introduction
Universal cache middleware for Hono powered by unstorage
hono-universal-cache
Universal cache middleware for Hono powered by unstorage.
Cache API responses across any runtime - Cloudflare Workers, Vercel Edge, Node.js, Bun, Deno, and more.
Optimized for API responses (JSON, text, HTML). For static assets (images, videos, files), use CDN/edge caching instead.
Features
- ⨠Universal Runtime Support - Works everywhere Hono works
- đī¸ Multiple Storage Drivers - Memory, Redis, Cloudflare KV, Vercel KV, filesystem, and more
- ⥠TTL Support - Automatic expiration with configurable time-to-live
- đ¯ Selective Caching - Control what gets cached by status code
- đ Custom Key Generation - Flexible cache key strategies
- đĒļ Lightweight - Minimal overhead, focused on storage operations
- đ¨ Simple & Predictable - No magic, just storage-based caching
- đĻ Efficient Storage - Optimized for text-based API responses
Installation
npm install hono-universal-cachepnpm add hono-universal-cacheyarn add hono-universal-cachebun add hono-universal-cacheunstorage is included as a dependency - no need to install it separately!
Quick Start
Basic Usage (In-Memory)
import { Hono } from "hono";
import { universalCache } from "hono-universal-cache";
const app = new Hono();
app.use(
"*",
universalCache({
cacheName: "my-app-cache",
ttl: 3600, // 1 hour
}),
);
app.get("/api/data", (c) => {
return c.json({ timestamp: Date.now() });
});
export default app;With Custom Storage Driver
import { createStorage } from "unstorage";
import redisDriver from "unstorage/drivers/redis";
const storage = createStorage({
driver: redisDriver({
host: "localhost",
port: 6379,
}),
});
app.use(
"*",
universalCache({
cacheName: "api-cache",
storage,
ttl: 3600,
}),
);