httpcache

package
v0.0.0-...-0b59bd9 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2014 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package httpcache provides facilities for caching HTTP responses and hypermedia for REST resources. The saved responses respect HTTP caching policies. Hypermedia shouldn't change, so is stored for as long as possible.

Index

Constants

This section is empty.

Variables

View Source
var DefaultExpirationDuration = time.Hour
View Source
var NoResponseError = errors.New("No Response")

Functions

func Encode

func Encode(res *sawyer.Response, writer io.Writer) error

Encode will create a CachedResponse from the sawyer Response, and encode it to the given writer.

func EncodeBody

func EncodeBody(res *sawyer.Response, bodyWriter io.Writer) error

EncodeBody copies the response's Body to the given writer.

func EncodeResponse

func EncodeResponse(cached *CachedResponse, writer io.Writer) error

EncodeResponse encodes the CachedResponse to the given writer.

func RequestKey

func RequestKey(r *http.Request) string

RequestKey builds a unique string key for a net/http Request.

func RequestSha

func RequestSha(r *http.Request) string

Types

type CachedResponse

type CachedResponse struct {
	Expires          time.Time
	Status           string // e.g. "200 OK"
	StatusCode       int    // e.g. 200
	Proto            string // e.g. "HTTP/1.0"
	ProtoMajor       int    // e.g. 1
	ProtoMinor       int    // e.g. 0
	Header           http.Header
	ContentLength    int64
	TransferEncoding []string
	Trailer          http.Header
	MediaType        mediatype.MediaType
}

CachedResponse is an http.Response that can be encoded and decoded safely.

type CachedResponseDecoder

type CachedResponseDecoder struct {
	Cacher      sawyer.Cacher
	SetBodyFunc func(res *sawyer.Response)
	*CachedResponse
}

CachedResponseDecoder can decode the embedded CachedResponse into a sawyer response.

func Decode

func Decode(reader io.Reader) (*CachedResponseDecoder, error)

Decode decodes the CachedResponse from the given reader. It is then wrapped by a CachedResponseDecoder that is able to turn the CachedResponse data to a sawyer Response.

func (*CachedResponseDecoder) Decode

func (r *CachedResponseDecoder) Decode(req *sawyer.Request) *sawyer.Response

Decode converts the embedded CachedResponse to a sawyer Response.

func (*CachedResponseDecoder) IsExpired

func (r *CachedResponseDecoder) IsExpired() bool

IsExpired returns true if the CachedResponse needs to be refreshed.

func (*CachedResponseDecoder) IsFresh

func (r *CachedResponseDecoder) IsFresh() bool

IsFresh returns true if the CachedResponse does not need to be refreshed.

func (*CachedResponseDecoder) SetupRequest

func (r *CachedResponseDecoder) SetupRequest(req *http.Request)

SetupRequest passes the cached ETag and Last Modified date to the request.

type FileCache

type FileCache struct {
	// contains filtered or unexported fields
}

FileCache is a sawyer.Cacher that stores entries on disk.

func NewFileCache

func NewFileCache(path string) *FileCache

func (*FileCache) Clear

func (c *FileCache) Clear(req *http.Request) error

func (*FileCache) Get

func (c *FileCache) Get(req *http.Request) (sawyer.CachedResponse, error)

func (*FileCache) Rels

func (c *FileCache) Rels(req *http.Request) (hypermedia.Relations, bool)

func (*FileCache) Reset

func (c *FileCache) Reset(req *http.Request) error

func (*FileCache) Set

func (c *FileCache) Set(req *http.Request, res *sawyer.Response) error

func (*FileCache) SetRels

func (c *FileCache) SetRels(req *http.Request, rels hypermedia.Relations) error

func (*FileCache) UpdateCache

func (c *FileCache) UpdateCache(req *http.Request, res *http.Response) error

type MemoryCache

type MemoryCache struct {
	Cache map[string]*cacheEntry
}

MemoryCache is a sawyer.Cacher that stores the entries in memory. This is only intended for testing, and should not be used in production.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(req *http.Request) error

func (*MemoryCache) Get

func (c *MemoryCache) Get(req *http.Request) (sawyer.CachedResponse, error)

func (*MemoryCache) Rels

func (c *MemoryCache) Rels(req *http.Request) (hypermedia.Relations, bool)

func (*MemoryCache) Reset

func (c *MemoryCache) Reset(req *http.Request) error

func (*MemoryCache) Set

func (c *MemoryCache) Set(req *http.Request, res *sawyer.Response) error

func (*MemoryCache) SetRels

func (c *MemoryCache) SetRels(req *http.Request, rels hypermedia.Relations) error

func (*MemoryCache) UpdateCache

func (c *MemoryCache) UpdateCache(req *http.Request, res *http.Response) error

Jump to

Keyboard shortcuts

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