Documentation ¶
Overview ¶
Package layer implements a cache layer which allows caching of complete responses.
Use New to initialize a new Layer and then call Wrap on any app.Handler to obtain a new app.Handler wrapped by the Layer.
A Mediator indicates the layer if a response should be cached and for how long, as well as indicating which requests should bypass the Layer.
This package provides the SimpleMediator, which implements the Mediator protocol with enough knobs to satisty most common needs. Users with more advanced requirements should write their own Mediator implementation.
cache, err := myapp.Cache() if err != nil { panic(err) } layer := layer.New(cache.Cache, &layer.SimpleMediator{Expiration:600}) myapp.Handle("/something/", layer.Wrap(MyHandler))
Index ¶
- type Layer
- type Mediator
- type SimpleMediator
- func (m *SimpleMediator) Cache(ctx *app.Context, responseCode int, outgoingHeaders http.Header) bool
- func (m *SimpleMediator) Expires(ctx *app.Context, responseCode int, outgoingHeaders http.Header) int
- func (m *SimpleMediator) Key(ctx *app.Context) string
- func (m *SimpleMediator) Skip(ctx *app.Context) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Layer ¶
type Layer struct {
// contains filtered or unexported fields
}
Layer allows caching complete responses to requests. Use New to initialize a Layer.
func (*Layer) Wrap ¶
Wrap takes a app.Handler and returns a new app.Handler wrapped by the Layer. Responses will be cached according to what the Layer's Mediator indicates. Note that when the environment variable GONDOLA_NO_CACHE_LAYER is non empty, Wrap returns the same app.Handler that was received (id est, it does nothing). This is done in order to simplify profiling Gondola apps (gondola dev -profile sets this environment variable).
type Mediator ¶
type Mediator interface { // Skip indicates if the request in the given context should skip the cache. Skip(ctx *app.Context) bool // Key returns the cache key used for the given context. Key(ctx *app.Context) string // Cache returns wheter a response with the given code and headers should // be cached. Cache(ctx *app.Context, responseCode int, outgoingHeaders http.Header) bool // Expires returns the cache expiration time for given context, response code // and headers. Expires(ctx *app.Context, responseCode int, outgoingHeaders http.Header) int }
Mediator is the interface which indicates the Layer if and how a response should be cached.
type SimpleMediator ¶
type SimpleMediator struct { // SkipCookies includes any cookie which should make the request // skip the cache Layer when the cookie is present. SkipCookies []string // Expiration indicates the cache expiration for cached requests. Expiration int }
SimpleMediator implements a Mediator which caches GET and HEAD request with a 200 response code for a fixed time and skips the cache if any of the indicated cookies are present. Cache keys are generated by hashing the request method and its URL.