Skip to content

Universal Cache Middleware with Storage Adapter Support #3857

@lord007tn

Description

@lord007tn

What is the feature you are proposing?

Problem

Hono's current cache middleware implementation has two key limitations:

  • Platform-specific dependency (Cloudflare/Deno only)
  • Lack of storage adapter support (Redis, Memory, Filesystem, etc.)

This forces developers to:

  • Write custom caching solutions for Node.js/Bun environments
  • Re-implement cache layers for different storage backends
  • Maintain inconsistent caching patterns across projects

Proposal: Universal Cache Middleware

Implement a flexible caching system inspired by Nitro's cache implementation with:

Storage Adapter Interface

Support pluggable adapters for:

  • Redis
  • In-memory (default)
  • Filesystem
  • Cloudflare KV/Deno KV
  • Custom implementations

Unified Configuration

// Global configuration
const app = new Hono({
  cache: {
    storage: 'redis',
    ttl: 3600,
    redisOptions: { ... }
  }
})

// Per-route configuration
app.get('/data', 
  cache({
    storage: 'memory',
    ttl: 60,
    vary: ['Authorization']
  }),
  handler
)

Core Features

  • TTL management
  • Cache-Control header synchronization
  • Vary header support
  • Cache key generation strategies
  • Multi-platform support (Node, Bun, Deno, Cloudflare, etc.)

Why This Matters

  • Framework Completeness: Modern frameworks like Next.js, Nuxt, and Astro include robust caching solutions. Hono needs equivalent capabilities to be considered production-ready for full-stack applications.
  • Developer Experience: Reduce boilerplate for common caching patterns:
// Current workaround
let data
if (await cache.exists(key)) {
  data = await cache.get(key)
} else {
  data = normalDataRetriving()
  await cache.set(key, data)
}

Performance Critical

Proper caching is essential for:

  • API response caching
  • SSR/SSG optimizations
  • Reducing expensive operations/API calls
  • Platform Agnosticism

Aligns with Hono's core philosophy of universal compatibility across runtimes.

Implementation Considerations

Storage Adapter Interface Proposal

interface CacheStorage {
  get(key: string): Promise<Response | null>
  set(key: string, response: Response, options?: CacheOptions): Promise<void>
  delete(key: string): Promise<void>
}

Priority Adapters

  • Memory (default)
  • Redis (with popular clients)
  • Filesystem (Node/Bun)
  • Cloudflare KV

Other considerations:

  • Automatic TTL handling from Cache-Control headers

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions