httpcache

package module
v0.0.0-...-1f4a71b Latest Latest
Warning

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

Go to latest
Published: May 28, 2022 License: MIT Imports: 11 Imported by: 0

README

httpcache

GoDoc

Package httpcache provides a http.RoundTripper implementation that works as a mostly RFC 7234 compliant cache for http responses. This incarnation of the library is an active fork of github.com/gregjones/httpcache which is unmaintained.

It is only suitable for use as a 'private' cache (i.e. for a web-browser or an API-client and not for a shared proxy).

Cache Backends

If you implement any other backend and wish it to be linked here, please send a PR editing this file.

License

Documentation

Overview

Package httpcache provides a http.RoundTripper implementation that works as a mostly RFC-compliant cache for http responses.

It is only suitable for use as a 'private' cache (i.e. for a web-browser or an API-client and not for a shared proxy).

Index

Constants

View Source
const (

	// XFromCache is the header added to responses that are returned from the cache
	XFromCache = "X-From-Cache"
)

Variables

View Source
var DefaultKeyFunc = func(req *http.Request) string {
	if req.Method == http.MethodGet {
		return req.URL.String()
	} else {
		return req.Method + " " + req.URL.String()
	}
}

DefaultKeyFunc returns the cache key for req

View Source
var ErrNoDateHeader = errors.New("no Date header")

ErrNoDateHeader indicates that the HTTP headers contained no Date header.

Functions

func CachedResponse

func CachedResponse(ctx context.Context, c Cache, key string, req *http.Request) (resp *http.Response, err error)

CachedResponse returns the cached http.Response for req if present, and nil otherwise.

func Date

func Date(respHeaders http.Header) (date time.Time, err error)

Date parses and returns the value of the Date header.

Types

type Cache

type Cache interface {
	// Get returns the []byte representation of a cached response and a bool
	// set to true if the value isn't empty
	Get(ctx context.Context, key string) (responseBytes []byte, ok bool)
	// Set stores the []byte representation of a response against a key
	Set(ctx context.Context, key string, responseBytes []byte)
	// Delete removes the value associated with the key
	Delete(ctx context.Context, key string)
}

A Cache interface is used by the Transport to store and retrieve responses.

type KeyFunc

type KeyFunc func(req *http.Request) string

type MemoryCache

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

MemoryCache is an implemtation of Cache that stores responses in an in-memory map.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache returns a new Cache that will store items in an in-memory map

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(ctx context.Context, key string)

Delete removes key from the cache

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string) (resp []byte, ok bool)

Get returns the []byte representation of the response and true if present, false if not

func (*MemoryCache) Set

func (c *MemoryCache) Set(ctx context.Context, key string, resp []byte)

Set saves response resp to the cache with key

type Transport

type Transport struct {
	// The RoundTripper interface actually used to make requests
	// If nil, http.DefaultTransport is used
	Transport http.RoundTripper
	Cache     Cache
	// If true, responses returned from the cache will be given an extra header, X-From-Cache
	MarkCachedResponses bool
	// A function to generate a cache key for the given request
	KeyFunc KeyFunc
}

Transport is an implementation of http.RoundTripper that will return values from a cache where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since) to repeated requests allowing servers to return 304 / Not Modified

func NewMemoryCacheTransport

func NewMemoryCacheTransport(opts ...TransportOpt) *Transport

NewMemoryCacheTransport returns a new Transport using the in-memory cache implementation

func NewTransport

func NewTransport(c Cache, opts ...TransportOpt) *Transport

NewTransport returns a new Transport with the provided Cache and options. If KeyFunc is not specified in opts then DefaultKeyFunc is used.

func (*Transport) Client

func (t *Transport) Client() *http.Client

Client returns an *http.Client that caches responses.

func (*Transport) RoundTrip

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

RoundTrip takes a Request and returns a Response

If there is a fresh Response already in cache, then it will be returned without connecting to the server.

If there is a stale Response, then any validators it contains will be set on the new request to give the server a chance to respond with NotModified. If this happens, then the cached Response will be returned.

type TransportOpt

type TransportOpt func(t *Transport)

TransportOpt is a configuration option for creating a new Transport

func KeyFuncOpt

func KeyFuncOpt(keyFunc KeyFunc) TransportOpt

KeyFuncOpt configures a transport by setting its KeyFunc to the one given

func MarkCachedResponsesOpt

func MarkCachedResponsesOpt(markCachedResponses bool) TransportOpt

MarkCachedResponsesOpt configures a transport by setting MarkCachedResponses to true

Directories

Path Synopsis
Package diskcache provides an implementation of httpcache.Cache that uses the diskv package to supplement an in-memory map with persistent storage
Package diskcache provides an implementation of httpcache.Cache that uses the diskv package to supplement an in-memory map with persistent storage
Package leveldbcache provides an implementation of httpcache.Cache that uses github.com/syndtr/goleveldb/leveldb
Package leveldbcache provides an implementation of httpcache.Cache that uses github.com/syndtr/goleveldb/leveldb
Package memcache provides an implementation of httpcache.Cache that uses gomemcache to store cached responses.
Package memcache provides an implementation of httpcache.Cache that uses gomemcache to store cached responses.
Package redis provides a redis interface for http caching.
Package redis provides a redis interface for http caching.

Jump to

Keyboard shortcuts

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