Closed robertyclin closed 1 week ago
I was looking into this myself and was wondering if there is a way to skip cache if the driver gives an error. If valuable I can make a reproduction, but it's quit simple just run nitro or nuxt and a redis instance and kill your redis instance after which defineCachedEventHandler will return an error.
Happy to help implementing this if somebody would point me in the right direction. Possible solution to add this manually is to use shouldBypassCache
but then I am unsure how I could check if redis (or any cache driver) is available at the time.
So something like this seems to work but feels like a workaround.
shouldBypassCache: async (event: H3Event) => {
try {
const cacheStorage = useStorage('cache:nitro');
await cacheStorage.getKeys();
return false;
} catch () {
return true;
}
}
This seems like quite an important issue, I'm surprised it hasn't got any love! I would also expect that should the caching mechanism fail it should fall back to calling the function and logging the error
For a reproduction you can use the starter app and adjust the config:
// https://nitro.unjs.io/config
export default defineNitroConfig({
srcDir: "server",
storage: {
redis: {
driver: "redis",
url: "redis://localhost:6379",
maxRetriesPerRequest: 3, // give up sooner and return an error
},
},
routeRules: {
"/**": { cache: { maxAge: 60 * 60, base: "redis" } },
},
});
@pi0 I think this can be marked as fixed
Describe the feature
defineCachedEventHandler works normally
Additional information