Documentation
¶
Overview ¶
Package httpcache provides an HTTP response cache layer over the generic core cache.Cache interface. It is shared by the forwarder and sniffing sniffers to cache upstream HTTP responses for reverse-proxy / CDN-mirror use cases (see tnet issue #349).
The generic cache stores opaque []byte; this package owns all HTTP policy (which methods/statuses are cacheable, per-status TTLs, serve-stale, body size limits) and the response (de)serialization via net/http.
Index ¶
- Constants
- func Key(method, host, uri string) string
- type Cache
- func (c *Cache) Cacheable(req *http.Request, resp *http.Response) bool
- func (c *Cache) CacheableRequest(req *http.Request) bool
- func (c *Cache) Lookup(ctx context.Context, req *http.Request) (resp *http.Response, stale bool, ok bool)
- func (c *Cache) MaxBodyBytes() int
- func (c *Cache) ServeStale() bool
- func (c *Cache) Store(ctx context.Context, req *http.Request, data []byte, status int) error
- func (c *Cache) StoreKey(ctx context.Context, key string, data []byte, status int) error
- func (c *Cache) TTLFor(status int) time.Duration
- func (c *Cache) TeeWriter(w io.Writer) *TeeWriter
- type Policy
- type TeeWriter
Constants ¶
const DefaultMaxBodyBytes = 1 << 20 // 1MB
DefaultMaxBodyBytes is the cache size cap applied when Policy.MaxBodyBytes is unset.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache wraps a core cache.Cache with HTTP-specific policy and serialization.
func FromMetadata ¶
FromMetadata builds an HTTP cache over store, reading its policy from handler metadata. Returns nil when store is nil (no cache configured). Shared by all handlers that support response caching so the metadata contract stays in one place.
func New ¶
New creates an HTTP cache over store with the given policy. Returns nil if store is nil so callers can treat "no cache" as a nil *Cache.
func (*Cache) Cacheable ¶
Cacheable reports whether the request/response pair may be stored: an eligible method plus a 2xx/3xx status.
func (*Cache) CacheableRequest ¶
CacheableRequest reports whether req's method is eligible for caching. Cheap pre-check so callers skip cache work for POST/etc.
func (*Cache) Lookup ¶
func (c *Cache) Lookup(ctx context.Context, req *http.Request) (resp *http.Response, stale bool, ok bool)
Lookup returns the cached response for req, whether it is stale (expired), and whether an entry was found. The returned response's Body must be closed by the caller. req is passed to http.ReadResponse so HEAD parses body-less.
func (*Cache) MaxBodyBytes ¶
MaxBodyBytes returns the configured serialized-response size cap.
func (*Cache) ServeStale ¶
ServeStale reports whether stale entries may be served on upstream failure.
func (*Cache) Store ¶
Store saves a serialized response (as produced by resp.Write) under req's key, using the TTL for status.
func (*Cache) StoreKey ¶
StoreKey saves data under an explicit key using the TTL for status. Callers whose request identity is mutated before the response is stored (e.g. Host rewritten for mirror routing) must capture the pre-rewrite key and pass it here, so Lookup and Store agree on the same key.
type Policy ¶
type Policy struct {
// DefaultTTL is the TTL for a cached response whose status has no explicit
// StatusTTL entry.
DefaultTTL time.Duration
// StatusTTL maps an HTTP status code to a TTL, overriding DefaultTTL.
StatusTTL map[int]time.Duration
// ServeStale serves an expired cached response when the upstream fetch
// fails.
ServeStale bool
// Methods is the set of cacheable request methods (upper-case). Defaults
// to GET and HEAD when empty.
Methods map[string]bool
// MaxBodyBytes is the maximum serialized response size to cache. Responses
// exceeding it are not cached. 0 means the DefaultMaxBodyBytes default.
MaxBodyBytes int
}
Policy holds the HTTP caching rules for a service.