Closed shell-skrimp closed 1 year ago
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
@shell-skrimp Do you have an example code?
@gaby All I do is enable the cache middleware and nothing more
d, err := time.ParseDuration(site.CacheDuration)
if err != nil {
return err
}
app.Use(cache.New(cache.Config{
ExpirationGenerator: func(c *fiber.Ctx, cfg *cache.Config) time.Duration {
newCacheTime, _ := strconv.Atoi(c.GetRespHeader("Cache-Control", fmt.Sprintf("max-age=%.0f", d.Seconds())))
return time.Second * time.Duration(newCacheTime)
},
KeyGenerator: func(c *fiber.Ctx) string {
return c.Path()
},
Next: func(c *fiber.Ctx) bool {
return c.Query("refresh") == "true"
},
MaxBytes: site.CacheSizeBytes,
Expiration: d,
CacheControl: true,
}))
@shell-skrimp error is that you use in your keyGenerator the path as key without creating a copy
at the end of the request and response process for this request, the path is released again, but is referenced by your use in the cache map and then this value is reused and adjusted
solution is to make a copy, as also in our documentation for the default config https://docs.gofiber.io/api/middleware/cache#default-config
more about the reference topic -> https://docs.gofiber.io/#zero-allocation
Bug Description
Requires the cache middleware to be enabled (see stacktrace below)
How to Reproduce
Unsure on how to exactly reproduce, the issue occurred after some time using the cache middle ware.
Expected Behavior
Expect no panic.
Fiber Version
v2.48.0
Code Snippet (optional)
No response
Checklist: