nitro icon indicating copy to clipboard operation
nitro copied to clipboard

I'm using the caching system of nitro, which use the redis driver. If my redis connection fails, is there a way to make defineCachedEventHandler not report an error and not use the cache?

Open robertyclin opened this issue 1 year ago • 2 comments

Describe the feature

defineCachedEventHandler works normally

Additional information

  • [ ] Would you be willing to help implement this feature?

robertyclin avatar Jul 12 '24 08:07 robertyclin

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;
    }
}

HPieters avatar Aug 12 '24 09:08 HPieters

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

bitbytebit1 avatar Oct 08 '24 01:10 bitbytebit1

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" } },
  },
});

cjpearson avatar Oct 29 '24 20:10 cjpearson

@pi0 I think this can be marked as fixed

cjpearson avatar Nov 06 '24 14:11 cjpearson