{"modulePath":"github.com/sourcegraph/httpcache","version":"v0.0.0-20160524185540-16db777d8ebe","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpcache\n\nPackage httpcache provides a http.RoundTripper implementation that works as a mostly RFC-compliant cache for http responses.\n\nIt 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).\n\n## Constants\n\n```go\nconst (\n\n\t// XFromCache is the header added to responses that are returned from the cache\n\tXFromCache = \"X-From-Cache\"\n)\n```\n\n## Variables\n\n```go\nvar ErrNoDateHeader = errors.New(\"no Date header\")\n```\nErrNoDateHeader indicates that the HTTP headers contained no Date header.\n\n## Functions\n\n```go\nfunc CachedResponse(c Cache, req *http.Request) (resp *http.Response, err error)\n```\nCachedResponse returns the cached http.Response for req if present, and nil otherwise.\n\n```go\nfunc Date(respHeaders http.Header) (date time.Time, err error)\n```\nDate parses and returns the value of the Date header.\n\n## Types\n\n```go\ntype Cache interface {\n\t// Get returns the []byte representation of a cached response and a bool\n\t// set to true if the value isn't empty\n\tGet(key string) (responseBytes []byte, ok bool)\n\t// Set stores the []byte representation of a response against a key\n\tSet(key string, responseBytes []byte)\n\t// Delete removes the value associated with the key\n\tDelete(key string)\n}\n```\nA Cache interface is used by the Transport to store and retrieve responses.\n\n```go\ntype MemoryCache struct {\n\t// contains filtered or unexported fields\n}\n```\nMemoryCache is an implemtation of Cache that stores responses in an in-memory map.\n\n```go\nfunc NewMemoryCache() *MemoryCache\n```\nNewMemoryCache returns a new Cache that will store items in an in-memory map\n\n```go\nfunc (c *MemoryCache) Delete(key string)\n```\nDelete removes key from the cache\n\n```go\nfunc (c *MemoryCache) Get(key string) (resp []byte, ok bool)\n```\nGet returns the \\[]byte representation of the response and true if present, false if not\n\n```go\nfunc (c *MemoryCache) Set(key string, resp []byte)\n```\nSet saves response resp to the cache with key\n\n```go\ntype Transport struct {\n\t// The RoundTripper interface actually used to make requests\n\t// If nil, http.DefaultTransport is used\n\tTransport http.RoundTripper\n\tCache     Cache\n\t// If true, responses returned from the cache will be given an extra header, X-From-Cache\n\tMarkCachedResponses bool\n\t// contains filtered or unexported fields\n}\n```\nTransport 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\n\n```go\nfunc NewMemoryCacheTransport() *Transport\n```\nNewMemoryCacheTransport returns a new Transport using the in-memory cache implementation\n\n```go\nfunc NewTransport(c Cache) *Transport\n```\nNewTransport returns a new Transport with the provided Cache implementation and MarkCachedResponses set to true\n\n```go\nfunc (t *Transport) CancelRequest(req *http.Request)\n```\nCancelRequest calls CancelRequest on the underlaying transport if implemented or throw a warning otherwise.\n\n```go\nfunc (t *Transport) Client() *http.Client\n```\nClient returns an \\*http.Client that caches responses.\n\n```go\nfunc (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error)\n```\nRoundTrip takes a Request and returns a Response\n\nIf there is a fresh Response already in cache, then it will be returned without connecting to the server.\n\nIf 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.\n\n","path":"github.com/sourcegraph/httpcache","name":"httpcache","synopsis":"Package httpcache provides a http.RoundTripper implementation that works as a mostly RFC-compliant cache for http responses.","isRedistributable":true}
