mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
This commit is contained in:
12
cache/cache.go
vendored
12
cache/cache.go
vendored
@@ -34,6 +34,18 @@ import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
)
|
||||
|
||||
// WithKey sets a custom entry key for cached pages.
|
||||
// Should be prepended to the cache handler.
|
||||
//
|
||||
// Usage:
|
||||
// app.Get("/", cache.WithKey("custom-key"), cache.Handler(time.Minute), mainHandler)
|
||||
func WithKey(key string) context.Handler {
|
||||
return func(ctx *context.Context) {
|
||||
client.SetKey(ctx, key)
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// Cache accepts the cache expiration duration.
|
||||
// If the "expiration" input argument is invalid, <=2 seconds,
|
||||
// then expiration is taken by the "cache-control's maxage" header.
|
||||
|
||||
50
cache/client/handler.go
vendored
50
cache/client/handler.go
vendored
@@ -1,6 +1,7 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -73,6 +74,48 @@ func parseLifeChanger(ctx *context.Context) entry.LifeChanger {
|
||||
}
|
||||
}
|
||||
|
||||
const entryKeyContextKey = "iris.cache.server.entry.key"
|
||||
|
||||
// SetKey sets a custom entry key for cached pages.
|
||||
// See root package-level `WithKey` instead.
|
||||
func SetKey(ctx *context.Context, key string) {
|
||||
ctx.Values().Set(entryKeyContextKey, key)
|
||||
}
|
||||
|
||||
// GetKey returns the entry key for the current page.
|
||||
func GetKey(ctx *context.Context) string {
|
||||
return ctx.Values().GetString(entryKeyContextKey)
|
||||
}
|
||||
|
||||
func getOrSetKey(ctx *context.Context) string {
|
||||
if key := GetKey(ctx); key != "" {
|
||||
return key
|
||||
}
|
||||
|
||||
// Note: by-default the rules(ruleset pkg)
|
||||
// explictly ignores the cache handler
|
||||
// execution on authenticated requests
|
||||
// and immediately runs the next handler:
|
||||
// if !h.rule.Claim(ctx) ...see `Handler` method.
|
||||
// So the below two lines are useless,
|
||||
// however we add it for cases
|
||||
// that the end-developer messedup with the rules
|
||||
// and by accident allow authenticated cached results.
|
||||
username, password, _ := ctx.Request().BasicAuth()
|
||||
authPart := username + strings.Repeat("*", len(password))
|
||||
|
||||
key := ctx.Method() + authPart
|
||||
|
||||
u := ctx.Request().URL
|
||||
if !u.IsAbs() {
|
||||
key += ctx.Scheme() + ctx.Host()
|
||||
}
|
||||
key += u.String()
|
||||
|
||||
SetKey(ctx, key)
|
||||
return key
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(ctx *context.Context) {
|
||||
// check for pre-cache validators, if at least one of them return false
|
||||
// for this specific request, then skip the whole cache
|
||||
@@ -90,16 +133,11 @@ func (h *Handler) ServeHTTP(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
scheme := "http"
|
||||
if ctx.Request().TLS != nil {
|
||||
scheme = "https"
|
||||
}
|
||||
|
||||
var (
|
||||
response *entry.Response
|
||||
valid = false
|
||||
// unique per subdomains and paths with different url query.
|
||||
key = scheme + ctx.Host() + ctx.Request().URL.RequestURI()
|
||||
key = getOrSetKey(ctx)
|
||||
)
|
||||
|
||||
h.mu.RLock()
|
||||
|
||||
Reference in New Issue
Block a user