Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 [Feature]: Cache-Control: no-cache #2159

Merged
merged 24 commits into from Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 37 additions & 33 deletions middleware/cache/cache.go
Expand Up @@ -109,43 +109,47 @@ func New(config ...Config) fiber.Handler {
// Get timestamp
ts := atomic.LoadUint64(&timestamp)

// Check if entry is expired
if e.exp != 0 && ts >= e.exp {
deleteKey(key)
if cfg.MaxBytes > 0 {
_, size := heap.remove(e.heapidx)
storedBytes -= size
}
} else if e.exp != 0 {
// Separate body value to avoid msgp serialization
// We can store raw bytes with Storage 馃憤
if cfg.Storage != nil {
e.body = manager.getRaw(key + "_body")
}
// Set response headers from cache
c.Response().SetBodyRaw(e.body)
c.Response().SetStatusCode(e.status)
c.Response().Header.SetContentTypeBytes(e.ctype)
if len(e.cencoding) > 0 {
c.Response().Header.SetBytesV(fiber.HeaderContentEncoding, e.cencoding)
}
if e.headers != nil {
for k, v := range e.headers {
c.Response().Header.SetBytesV(k, v)
// Check if no-cache
if !cfg.noCache(c) {
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved

// Check if entry is expired
if e.exp != 0 && ts >= e.exp {
deleteKey(key)
if cfg.MaxBytes > 0 {
_, size := heap.remove(e.heapidx)
storedBytes -= size
}
} else if e.exp != 0 {
// Separate body value to avoid msgp serialization
// We can store raw bytes with Storage 馃憤
if cfg.Storage != nil {
e.body = manager.getRaw(key + "_body")
}
// Set response headers from cache
c.Response().SetBodyRaw(e.body)
c.Response().SetStatusCode(e.status)
c.Response().Header.SetContentTypeBytes(e.ctype)
if len(e.cencoding) > 0 {
c.Response().Header.SetBytesV(fiber.HeaderContentEncoding, e.cencoding)
}
if e.headers != nil {
for k, v := range e.headers {
c.Response().Header.SetBytesV(k, v)
}
}
// Set Cache-Control header if enabled
if cfg.CacheControl {
maxAge := strconv.FormatUint(e.exp-ts, 10)
c.Set(fiber.HeaderCacheControl, "public, max-age="+maxAge)
}
}
// Set Cache-Control header if enabled
if cfg.CacheControl {
maxAge := strconv.FormatUint(e.exp-ts, 10)
c.Set(fiber.HeaderCacheControl, "public, max-age="+maxAge)
}

c.Set(cfg.CacheHeader, cacheHit)
c.Set(cfg.CacheHeader, cacheHit)

mux.Unlock()
mux.Unlock()

// Return response
return nil
// Return response
return nil
}
}

// make sure we're not blocking concurrent requests - do unlock
Expand Down
8 changes: 8 additions & 0 deletions middleware/cache/config.go
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"fmt"
"strings"
"time"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -72,6 +73,9 @@ type Config struct {
//
// Default: []string{fiber.MethodGet, fiber.MethodHead}
Methods []string

// If no-cache exist in request header
noCache func(c *fiber.Ctx) bool
efectn marked this conversation as resolved.
Show resolved Hide resolved
}

// ConfigDefault is the default config
Expand All @@ -88,6 +92,9 @@ var ConfigDefault = Config{
Storage: nil,
MaxBytes: 0,
Methods: []string{fiber.MethodGet, fiber.MethodHead},
noCache: func(c *fiber.Ctx) bool {
return strings.Contains(c.Get(fiber.HeaderCacheControl), "no-cache")
},
}

// Helper function to set default values
Expand Down Expand Up @@ -124,5 +131,6 @@ func configDefault(config ...Config) Config {
if len(cfg.Methods) == 0 {
cfg.Methods = ConfigDefault.Methods
}
cfg.noCache = ConfigDefault.noCache
return cfg
}