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 ¶
- Variables
- func Encode(res *sawyer.Response, writer io.Writer) error
- func EncodeBody(res *sawyer.Response, bodyWriter io.Writer) error
- func EncodeResponse(cached *CachedResponse, writer io.Writer) error
- func RequestKey(r *http.Request) string
- func RequestSha(r *http.Request) string
- type CachedResponse
- type CachedResponseDecoder
- type FileCache
- func (c *FileCache) Clear(req *http.Request) error
- func (c *FileCache) Get(req *http.Request) (sawyer.CachedResponse, error)
- func (c *FileCache) Rels(req *http.Request) (hypermedia.Relations, bool)
- func (c *FileCache) Reset(req *http.Request) error
- func (c *FileCache) Set(req *http.Request, res *sawyer.Response) error
- func (c *FileCache) SetRels(req *http.Request, rels hypermedia.Relations) error
- func (c *FileCache) UpdateCache(req *http.Request, res *http.Response) error
- type MemoryCache
- func (c *MemoryCache) Clear(req *http.Request) error
- func (c *MemoryCache) Get(req *http.Request) (sawyer.CachedResponse, error)
- func (c *MemoryCache) Rels(req *http.Request) (hypermedia.Relations, bool)
- func (c *MemoryCache) Reset(req *http.Request) error
- func (c *MemoryCache) Set(req *http.Request, res *sawyer.Response) error
- func (c *MemoryCache) SetRels(req *http.Request, rels hypermedia.Relations) error
- func (c *MemoryCache) UpdateCache(req *http.Request, res *http.Response) error
Constants ¶
This section is empty.
Variables ¶
var DefaultExpirationDuration = time.Hour
var NoResponseError = errors.New("No Response")
Functions ¶
func Encode ¶
Encode will create a CachedResponse from the sawyer Response, and encode it to the given writer.
func EncodeBody ¶
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 ¶
RequestKey builds a unique string key for a net/http Request.
func RequestSha ¶
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 ¶
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) 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) SetRels ¶
func (c *MemoryCache) SetRels(req *http.Request, rels hypermedia.Relations) error