Repository: gofiber/fiber
File: middleware/cache/cache.go (Go)

Code before:
const (
	cacheUnreachable = "unreachable"
	cacheHit         = "hit"
	cacheMiss        = "miss"
)

// directives
const (
	noCache = "no-cache"
	noStore = "no-store"
)

var ignoreHeaders = map[string]any{
	"Connection":          nil,
	"Keep-Alive":          nil,
	"Proxy-Authenticate":  nil,
	"Proxy-Authorization": nil,
	"TE":                  nil,
	"Trailers":            nil,
	"Transfer-Encoding":   nil,
	"Upgrade":             nil,
	"Content-Type":        nil, // already stored explicitly by the cache manager
	"Content-Encoding":    nil, // already stored explicitly by the cache manager
}

var cacheableStatusCodes = []int{
	fiber.StatusOK, fiber.StatusNonAuthoritativeInformation, fiber.StatusNoContent, fiber.StatusPartialContent,
	fiber.StatusMultipleChoices, fiber.StatusMovedPermanently,
	fiber.StatusNotFound, fiber.StatusMethodNotAllowed, fiber.StatusGone, fiber.StatusTeapot, fiber.StatusRequestURITooLong,
	fiber.StatusNotImplemented,
}

// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
	// Set default config
	cfg := configDefault(config...)

	// Nothing to cache
	if int(cfg.Expiration.Seconds()) < 0 {
		return func(c fiber.Ctx) error {
			return c.Next()
		}
	}

	var (
		// Cache settings
		mux       = &sync.RWMutex{}
		timestamp = uint64(time.Now().Unix()) //nolint:gosec //Not a concern
	)
	// Create manager to simplify storage operations ( see manager.go )
	manager := newManager(cfg.Storage)

Review comment (security):
This should be a map, for o(1) lookups. Ex:

```go
var cacheableStatusCodes = map[int]bool{
    fiber.StatusOK:                          true,
    fiber.StatusNonAuthoritativeInformation: true,
    // etc...
}
```

Code after:
const (
	cacheUnreachable = "unreachable"
	cacheHit         = "hit"
	cacheMiss        = "miss"
)

// directives
const (
	noCache = "no-cache"
	noStore = "no-store"
)

var ignoreHeaders = map[string]any{
	"Connection":          nil,
	"Keep-Alive":          nil,
	"Proxy-Authenticate":  nil,
	"Proxy-Authorization": nil,
	"TE":                  nil,
	"Trailers":            nil,
	"Transfer-Encoding":   nil,
	"Upgrade":             nil,
	"Content-Type":        nil, // already stored explicitly by the cache manager
	"Content-Encoding":    nil, // already stored explicitly by the cache manager
}

var cacheableStatusCodes = map[int]bool{
	fiber.StatusOK:                          true,
	fiber.StatusNonAuthoritativeInformation: true,
	fiber.StatusNoContent:                   true,
	fiber.StatusPartialContent:              true,
	fiber.StatusMultipleChoices:             true,
	fiber.StatusMovedPermanently:            true,
	fiber.StatusNotFound:                    true,
	fiber.StatusMethodNotAllowed:            true,
	fiber.StatusGone:                        true,
	fiber.StatusRequestURITooLong:           true,
	fiber.StatusTeapot:                      true,
	fiber.StatusNotImplemented:              true,
}

// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
	// Set default config
	cfg := configDefault(config...)

	// Nothing to cache
	if int(cfg.Expiration.Seconds()) < 0 {
		return func(c fiber.Ctx) error {
			return c.Next()
		}
	}