hono-universal-cache

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-cache
pnpm add hono-universal-cache
yarn add hono-universal-cache
bun add hono-universal-cache

unstorage 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,
  }),
);

On this page