honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
18.51k stars 522 forks source link

Add cache expiration time #3340

Open ryuapp opened 2 weeks ago

ryuapp commented 2 weeks ago

What is the feature you are proposing?

Currently, with cache middleware, once it is cached, it is never updated. I'd like to keep the cache on the server side for short periods of time, but that's currently cumbersome. So, we introduce the concept of expiration time in cache middleware.

Add the duration option and store it in an internal header when the cache matches and check it, as in the example below.

app.use(cache({
    cacheName: 'my-app',
    cacheControl: 'max-age=60',
    duration: 120 // new option
    wait: true,
}))
ryuapp commented 2 weeks ago

Just to add some background information, it's fair to say that cache middleware won't work properly with Deno because Cloudflare Workers respects the Cache-Control header, but Deno doesn't.

That's why we need an option like this.

Extarys commented 3 days ago

I don't use Deno, but wouldn't it make more sense to make Deno respect the Cache-Control header with a Deno setting instead ? That is probably not what the issue is about though :thinking:

ryuapp commented 3 days ago

I don't use Deno, but wouldn't it make more sense to make Deno respect the Cache-Control header with a Deno setting instead ? That is probably not what the issue is about though 🤔

Deno is just a JavaScript runtime and will not extend Cache API. Since Cloudflare Workers is a service with CDN, it seems to be natural to consider extending the API.