httpcache

package
v0.28.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const DefaultMaxBodyBytes = 1 << 20 // 1MB

DefaultMaxBodyBytes is the cache size cap applied when Policy.MaxBodyBytes is unset.

Variables

This section is empty.

Functions

func Key

func Key(method, host, uri string) string

Key builds the cache key from the request line: "GET example.com/api/users".

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

func FromMetadata(store cache.Cache, md mdata.Metadata) *Cache

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

func New(store cache.Cache, policy Policy) *Cache

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

func (c *Cache) Cacheable(req *http.Request, resp *http.Response) bool

Cacheable reports whether the request/response pair may be stored: an eligible method plus a 2xx/3xx status.

func (*Cache) CacheableRequest

func (c *Cache) CacheableRequest(req *http.Request) bool

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

func (c *Cache) MaxBodyBytes() int

MaxBodyBytes returns the configured serialized-response size cap.

func (*Cache) ServeStale

func (c *Cache) ServeStale() bool

ServeStale reports whether stale entries may be served on upstream failure.

func (*Cache) Store

func (c *Cache) Store(ctx context.Context, req *http.Request, data []byte, status int) error

Store saves a serialized response (as produced by resp.Write) under req's key, using the TTL for status.

func (*Cache) StoreKey

func (c *Cache) StoreKey(ctx context.Context, key string, data []byte, status int) error

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.

func (*Cache) TTLFor

func (c *Cache) TTLFor(status int) time.Duration

TTLFor returns the TTL to apply for a response with the given status.

func (*Cache) TeeWriter

func (c *Cache) TeeWriter(w io.Writer) *TeeWriter

TeeWriter wraps w so bytes written to it are also captured for caching, up to MaxBodyBytes. If the cap is exceeded, capture is abandoned (Captured returns nil) so oversized responses aren't cached — the client write is unaffected either way.

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.

type TeeWriter

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

TeeWriter captures a bounded copy of everything written through it.

func (*TeeWriter) Captured

func (t *TeeWriter) Captured() []byte

Captured returns the captured response bytes, or nil if the cap was exceeded (response too large to cache).

func (*TeeWriter) Write

func (t *TeeWriter) Write(p []byte) (int, error)

Write forwards p to the underlying writer and, unless the cap is exceeded, captures it. The client write always happens first and its result is returned verbatim.

Jump to

Keyboard shortcuts

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