Skip to content

Commit

Permalink
Added SetCacheControl method to request with relative constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide bonomini committed Dec 18, 2023
1 parent 81ea50b commit 7f1f3e2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions evo.constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,14 @@ const (
HeaderXRobotsTag = "X-Robots-Tag"
HeaderXUACompatible = "X-UA-Compatible"
)

// Cache control headers
const (
CachePrivate = "private"
CachePublic = "public"
CacheNoStore = "no-store"
CacheNoCache = "no-cache"
CacheNoTransform = "no-transform"
CacheMustUnderstand = "must-understand"
CacheImmutable = "immutable"
)
17 changes: 17 additions & 0 deletions evo.context.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,20 @@ func (r *Request) Var(key string, value ...any) generic.Value {
func (r *Request) RestartRouting() error {
return r.Context.RestartRouting()
}

func (r *Request) SetCacheControl(t time.Duration, headers ...string) {
if len(headers) == 0 {
return
}

var ccHeader string = fmt.Sprintf("max-age=%.0f", t.Seconds())
var options string

for _, header := range headers {
options = options + fmt.Sprintf(", %s", header)
}

ccHeader = ccHeader + options

r.SetHeader("Cache-Control", ccHeader)
}

0 comments on commit 7f1f3e2

Please sign in to comment.