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

🤗 [Question]: Cache Next is not called when KeyGenerator is Defined #3034

Closed
3 tasks done
hcancelik opened this issue Jun 11, 2024 · 6 comments · Fixed by #3036
Closed
3 tasks done

🤗 [Question]: Cache Next is not called when KeyGenerator is Defined #3034

hcancelik opened this issue Jun 11, 2024 · 6 comments · Fixed by #3036

Comments

@hcancelik
Copy link
Contributor

hcancelik commented Jun 11, 2024

Question Description

Is it by design or a bug that the Next function is not called when the KeyGenerator config is defined?

Code Snippet (optional)

package main

import "github.com/gofiber/fiber/v2"
import "github.com/gofiber/fiber/v2/middleware/cache"
import "log"

func main() {
  app := fiber.New()

  app.Use(cache.New(cache.Config{
    Next: func(c *fiber.Ctx) bool {
      panic("not called")
      return c.Query("noCache") == "true"
    },
    KeyGenerator: func(c *fiber.Ctx) string {
      return fmt.Sprintf("report-%s", c.Query("startDate"))
    },
    Expiration: 30 * time.Minute,
    CacheControl: false,
  }))

  log.Fatal(app.Listen(":3000"))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Jun 11, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

@hcancelik there is no dependency on the other function outside

but there are other dependencies, e.g. if the cache entry is not there and has to be created first, etc.

can you narrow down the problem more precisely?

if cfg.Next != nil && cfg.Next(c) {

@hcancelik
Copy link
Contributor Author

@ReneWerner87, thanks for your response.

I think I misunderstood what Next was doing. It looks like Next closure is not called if there is already a cache.

I was trying to skip the existing cache and generate a new one by passing query parameters with Next. I guess that's not doable with the current middleware.

Skip middleware completely skips the cache so I cannot use that either.

@ReneWerner87
Copy link
Member

@hcancelik good point, we are currently working on version 3

can you help and make the cache middleware capable of covering this case

i think we have had several requests like this from other users

maybe we should provide a cacheInvalidation function that gets the context and if it is true, the cache is invalidated and the caching is skipped

@hcancelik
Copy link
Contributor Author

@ReneWerner87 I'm not good at Go, but I will try it and let you know.

@hcancelik
Copy link
Contributor Author

@ReneWerner87 I just sent a PR for this #3036

@ReneWerner87 ReneWerner87 linked a pull request Jun 15, 2024 that will close this issue
16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants