mirror of https://github.com/gofiber/fiber.git
🩹 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
parent
46fffe4397
commit
23bcbd3324
|
@ -3,6 +3,7 @@
|
||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -95,22 +96,17 @@ func New(config ...Config) fiber.Handler {
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only cache selected methods
|
requestMethod := c.Method()
|
||||||
var isExists bool
|
|
||||||
for _, method := range cfg.Methods {
|
|
||||||
if c.Method() == method {
|
|
||||||
isExists = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isExists {
|
// Only cache selected methods
|
||||||
|
if !slices.Contains(cfg.Methods, requestMethod) {
|
||||||
c.Set(cfg.CacheHeader, cacheUnreachable)
|
c.Set(cfg.CacheHeader, cacheUnreachable)
|
||||||
return c.Next()
|
return c.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get key from request
|
// Get key from request
|
||||||
// TODO(allocation optimization): try to minimize the allocation from 2 to 1
|
// 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
|
// Get entry from pool
|
||||||
e := manager.get(key)
|
e := manager.get(key)
|
||||||
|
|
Loading…
Reference in New Issue