Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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? #2609

Open
1 task
robertyclin opened this issue Jul 12, 2024 · 3 comments

Comments

@robertyclin
Copy link

Describe the feature

defineCachedEventHandler works normally

Additional information

  • Would you be willing to help implement this feature?
@HPieters
Copy link

HPieters commented Aug 12, 2024

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

@bitbytebit1
Copy link

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

@cjpearson
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants