🩹 Fix: Optimize Cache middleware handler (#3031)

* Optimize cache handler

* revert to cfg.KeyGenerator(c) + _ + requestMethod

---------

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
pull/3035/head
Orville Simba 2024-06-12 22:22:36 +08:00 committed by GitHub
parent 46fffe4397
commit 23bcbd3324
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 9 deletions

View File

@ -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)