Skip to content
Discussion options

You must be logged in to vote

There's no "Hono shutdown event" to hook, and that's by design — Hono is just a request handler, it doesn't own the server or the process lifecycle. Middleware runs per request, so it's the wrong place to do one-time teardown. The cleanup belongs where your resource is created (once, in the closure — which you're already doing correctly) and runs at the runtime level on shutdown.

Keep the client in the closure as you have it, but expose it so you can close it from a signal handler:

export function cacheMiddleware(opts?: RedisOptions) {
  const cache = new Redis(opts)           // created once, when middleware is registered
  const mw = createMiddleware(async (c, next) => {
    c.set('cache',

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@seepine
Comment options

Answer selected by seepine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants