layer

package
v0.0.0-...-ba7cdbd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2014 License: MPL-2.0 Imports: 11 Imported by: 0

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

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 New

func New(c *cache.Cache, m Mediator) (*Layer, error)

New returns a new layer, returning only errors if the cache or the mediator are nil

func (*Layer) Cache

func (la *Layer) Cache() *cache.Cache

Cache returns the Layer's cache.

func (*Layer) Mediator

func (la *Layer) Mediator() Mediator

Mediator returns the Layer's mediator.

func (*Layer) Wrap

func (la *Layer) Wrap(handler app.Handler) app.Handler

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.

func (*SimpleMediator) Cache

func (m *SimpleMediator) Cache(ctx *app.Context, responseCode int, outgoingHeaders http.Header) bool

func (*SimpleMediator) Expires

func (m *SimpleMediator) Expires(ctx *app.Context, responseCode int, outgoingHeaders http.Header) int

func (*SimpleMediator) Key

func (m *SimpleMediator) Key(ctx *app.Context) string

func (*SimpleMediator) Skip

func (m *SimpleMediator) Skip(ctx *app.Context) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL