hono icon indicating copy to clipboard operation
hono copied to clipboard

Universal Cache Middleware with Storage Adapter Support

Open lord007tn opened this issue 11 months ago • 11 comments

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

lord007tn avatar Jan 26 '25 14:01 lord007tn

@yusukebe @sor4chi Would you please give your thoughts on this. I am willing to implement it. I have a basic implementation that I developed in our startup and maybe with your help, we can make it public.

lord007tn avatar Jan 29 '25 14:01 lord007tn

Hi @lord007tn

Interesting!

However, I don't want to add more logic depending on each runtime to this honojs/hono. That will increase the package size and maintenance costs. It will be okay to provide a super thin API or a TypeScript interface. For example, make Cache Middleware so that the user can replace the cache method instead of Cache API's.

yusukebe avatar Feb 02 '25 02:02 yusukebe

I am not sure i fully understand your idea. Could you provide for e.g a pseudo-code implementation so we can be on the same level. also, we can do it on their middleware repo so it can be installed when needed.

lord007tn avatar Feb 02 '25 05:02 lord007tn

Hi @lord007tn and @yusukebe, I'm very interested in this proposal too! I believe a universal cache middleware with adapter support would be an amazing addition to Hono. I'm willing to help contribute to the development if needed — either with implementation, reviewing, or testing. Please let me know how I can assist!

nferreira1 avatar Apr 28 '25 03:04 nferreira1

I think it will be better if we make it as third-party middleware like @hono/cache on https://github.com/honojs/middleware because we need to add tests using external libraries and middleware like Redis, Cloudflare KV, etc. We have to keep this honojs/hono repo small and does not depend on more external things. But making it on honojs/middleware will be okay.

yusukebe avatar Apr 28 '25 05:04 yusukebe

This is an excellent suggestion. The built-in Cache middleware should support Node.js and Bun environments to minimize differences. The default storage is an in-memory adapter, with support for Redis, Filesystem, and other storage adapters via third-party plugins.

drag0n-app avatar Jun 16 '25 07:06 drag0n-app

Why not building upon Unstorage https://unstorage.unjs.io/ for this? Maybe it even makes sense to adapt the same synax used in Nitro found at https://nitro.build/guide/cache.

leonceaklin avatar Jul 08 '25 15:07 leonceaklin

+1

Pyakz avatar Oct 16 '25 06:10 Pyakz

I am interested in this as well

Duzbee avatar Nov 01 '25 22:11 Duzbee

I commented before: https://github.com/honojs/hono/issues/3857#issuecomment-2834057938. I think it's better to implement this into honojs/middleware as an independent package like @hono/universal-cache. If someone does, I'll review it.

yusukebe avatar Nov 01 '25 22:11 yusukebe

I commented before: #3857 (comment). I think it's better to implement this into honojs/middleware as an independent package like @hono/universal-cache. If someone does, I'll review it.

@lord007tn @nferreira1 Can you implement this feature?

drag0n-app avatar Nov 06 '25 01:11 drag0n-app