From 23bcbd3324cfb98e4110331c481579bee72aaf1e Mon Sep 17 00:00:00 2001 From: Orville Simba <169886054+orvillesimba@users.noreply.github.com> Date: Wed, 12 Jun 2024 22:22:36 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix:=20Optimize=20Cache=20middle?= =?UTF-8?q?ware=20handler=20(#3031)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Optimize cache handler * revert to cfg.KeyGenerator(c) + _ + requestMethod --------- Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> --- middleware/cache/cache.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/middleware/cache/cache.go b/middleware/cache/cache.go index 0ace704e35..eabe94dafb 100644 --- a/middleware/cache/cache.go +++ b/middleware/cache/cache.go @@ -3,6 +3,7 @@ package cache import ( + "slices" "strconv" "strings" "sync" @@ -95,22 +96,17 @@ func New(config ...Config) fiber.Handler { return c.Next() } - // Only cache selected methods - var isExists bool - for _, method := range cfg.Methods { - if c.Method() == method { - isExists = true - } - } + requestMethod := c.Method() - if !isExists { + // Only cache selected methods + if !slices.Contains(cfg.Methods, requestMethod) { c.Set(cfg.CacheHeader, cacheUnreachable) return c.Next() } // Get key from request // TODO(allocation optimization): try to minimize the allocation from 2 to 1 - key := cfg.KeyGenerator(c) + "_" + c.Method() + key := cfg.KeyGenerator(c) + "_" + requestMethod // Get entry from pool e := manager.get(key)