Documentation ¶
Overview ¶
Package httpcache provides a http.RoundTripper wrapper implementation that works as a mostly RFC-compliant cached client 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 ¶
const (
// XFromCache is the header added to responses that are returned from the cache
XFromCache = "X-From-Cache"
)
Variables ¶
var ErrNoDateHeader = errors.New("no Date header")
ErrNoDateHeader indicates that the HTTP headers contained no Date header.
Functions ¶
func CachedResponse ¶
CachedResponse returns the cached http.Response for req if present, and nil otherwise.
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(key string) (responseBytes []byte, ok bool) // Set stores the []byte representation of a response for a given key, with a TTL for supporting implementations Set(key string, responseBytes []byte, ttl int) // Delete removes the value associated with the key Delete(key string) }
A Cache interface is used by the CachedClient to store and retrieve responses.
type CacheOptions ¶
type CachedClient ¶
type CachedClient struct { Transport http.RoundTripper Cache Cache Options CacheOptions }
CachedClient 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 (*CachedClient) Do ¶
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 ClientOptions ¶
type ClientOptions struct { }
type Doer ¶
A Doer interface abstracts the http.Client request execution from the client implementation
func NewCachedClient ¶
func NewCachedClient(client *http.Client, c Cache, options CacheOptions) Doer
NewCachedClient returns a new Transport with the provided Cache implementation and MarkCachedResponses set to true
func NewMapCachedClient ¶
NewMemoryCachedClient returns a new Transport using the in-memory map cache implementation
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is an implementation 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 (mc *MemoryCache) Delete(key string)
Delete removes key from the cache