cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2017 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cacheable

func Cacheable(req *http.Request, resp *http.Response) bool

Cacheable checks if the req/resp pair is cacheable based on https://tools.ietf.org/html/rfc7234#section-3

Types

type Cache

type Cache struct {
	sync.RWMutex
	URLVars map[URLKey]*Variations
	History *list.List
	Max     uint64
}

Cache stores pairs of requests/responses.

func (*Cache) Get

func (c *Cache) Get(req *http.Request) *CachedResponse

Get retrieves a cached response.

func (*Cache) Set

func (c *Cache) Set(req *http.Request, cached *CachedResponse)

Set inserts/updates a new pair of request/response to the cache.

type CachedResponse

type CachedResponse struct {
	sync.RWMutex
	Header       http.Header
	Body         []byte
	RequestTime  time.Time
	ResponseTime time.Time
	Element      *list.Element
}

CachedResponse represents a cached HTTP response.

func NewCachedResponse

func NewCachedResponse(resp *http.Response, reqTime, respTime time.Time) (*CachedResponse, error)

NewCachedResponse constructs a new cached response.

func (*CachedResponse) Response

func (e *CachedResponse) Response() *http.Response

Response converts a cached response to an HTTP response.

type CachedState

type CachedState int

CachedState represents freshness of a cached response.

const (
	// Miss means it's not in the cache.
	Miss CachedState = iota

	// Fresh means it has a cached response and it's available.
	Fresh

	// Stale means it has a cached response but it's not recommended.
	Stale

	// Revalidate means it has a cached response but needs confirmation from the backend.
	Revalidate
)

type Transport

type Transport struct {
	http.RoundTripper
	Cache
	OriginChangedAt time.Time
}

Transport is a caching round tripper.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip returns a cached response if found. Otherwise, retrieve one from the underlying transport.

func (*Transport) State

func (t *Transport) State(req *http.Request, cached *CachedResponse) (CachedState, time.Duration)

State returns the state of cached response.

type URLKey

type URLKey struct {
	Method string
	Host   string
	Path   string
	Query  string
}

URLKey identifies cached responses with the same URL.

func NewURLKey

func NewURLKey(req *http.Request) URLKey

NewURLKey returns a primary key of the request.

type VarKey

type VarKey string

VarKey identifies a cached response in variations.

func NewVarKey

func NewVarKey(pe *Variations, req *http.Request) VarKey

NewVarKey constructs a new variation key from a request.

type Variations

type Variations struct {
	Fields      []string
	VarResponse map[VarKey]*CachedResponse
}

Variations represents cached responses with the same URL.

func NewVariations

func NewVariations(resp *CachedResponse) *Variations

NewVariations constructs a new variations for the cached response.

Jump to

Keyboard shortcuts

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