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 ¶
const ( // XFromCache is the header added to responses that are returned from the cache XFromCache = "X-From-Cache" // XRevalidated is the header added to responses that got revalidated XRevalidated = "X-Revalidated" // XStale is the header added to responses that are stale XStale = "X-Stale" // XFreshness is the header added to responses indicating the freshness state XFreshness = "X-Cache-Freshness" // XCachedTime is the internal header used to store when a response was cached XCachedTime = "X-Cached-Time" )
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 against a key
Set(key string, responseBytes []byte)
// Delete removes the value associated with the key
Delete(key string)
}
A Cache interface is used by the Transport to store and retrieve responses.
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(key string)
Delete removes key from the cache
func (*MemoryCache) Get ¶
func (c *MemoryCache) Get(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(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
// If true, server errors (5xx status codes) will not be served from cache
// even if they are fresh. This forces a new request to the server.
// Default is false to maintain backward compatibility.
SkipServerErrorsFromCache bool
// AsyncRevalidateTimeout is the context timeout for async requests triggered by stale-while-revalidate.
// If zero, no timeout is applied to async revalidation requests.
AsyncRevalidateTimeout time.Duration
// ShouldCache allows configuring non-standard caching behaviour based on the response.
// If set, this function is called to determine whether a non-200 response should be cached.
// This enables caching of responses like 404 Not Found, 301 Moved Permanently, etc.
// If nil, only 200 OK responses are cached (standard behavior).
// The function receives the http.Response and should return true to cache it.
// Note: This only bypasses the status code check; Cache-Control headers are still respected.
ShouldCache func(*http.Response) bool
// CacheKeyHeaders specifies additional request headers to include in the cache key generation.
// This allows creating separate cache entries based on request header values.
// Common use cases include "Authorization" for user-specific caches or "Accept-Language"
// for locale-specific responses.
// Header names are case-insensitive and will be canonicalized.
// Example: []string{"Authorization", "Accept-Language"}
// Note: This is different from the HTTP Vary response header mechanism, which is handled separately.
CacheKeyHeaders []string
}
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() *Transport
NewMemoryCacheTransport returns a new Transport using the in-memory cache implementation
func NewTransport ¶
NewTransport returns a new Transport with the provided Cache implementation and MarkCachedResponses set to true
func (*Transport) RoundTrip ¶
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.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package blobcache provides an httpcache.Cache implementation that uses Go Cloud Development Kit (CDK) blob storage for cloud-agnostic cache storage.
|
Package blobcache provides an httpcache.Cache implementation that uses Go Cloud Development Kit (CDK) blob storage for cloud-agnostic cache storage. |
|
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 |
|
examples
|
|
|
basic
command
|
|
|
blobcache
command
|
|
|
cachekeyheaders
command
|
|
|
compresscache
command
|
|
|
custom-backend
command
|
|
|
diskcache
command
|
|
|
freecache
command
|
|
|
hazelcast
command
|
|
|
leveldb
command
|
|
|
mongodb
command
|
|
|
multicache
command
|
|
|
natskv
command
|
|
|
postgresql
command
|
|
|
prometheus
command
|
|
|
redis
command
|
|
|
security-best-practices
command
|
|
|
Package freecache provides a high-performance, zero-GC overhead implementation of httpcache.Cache using github.com/coocood/freecache as the underlying storage.
|
Package freecache provides a high-performance, zero-GC overhead implementation of httpcache.Cache using github.com/coocood/freecache as the underlying storage. |
|
Package hazelcast provides a Hazelcast interface for http caching.
|
Package hazelcast provides a Hazelcast interface for http caching. |
|
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 mongodb provides a MongoDB interface for http caching.
|
Package mongodb provides a MongoDB interface for http caching. |
|
Package natskv provides a NATS JetStream Key/Value store interface for http caching.
|
Package natskv provides a NATS JetStream Key/Value store interface for http caching. |
|
Package postgresql provides a PostgreSQL interface for HTTP caching.
|
Package postgresql provides a PostgreSQL interface for HTTP caching. |
|
Package redis provides a redis interface for http caching.
|
Package redis provides a redis interface for http caching. |
|
wrapper
|
|
|
compresscache
Package compresscache provides a cache wrapper that automatically compresses cached data to reduce storage requirements and network bandwidth usage.
|
Package compresscache provides a cache wrapper that automatically compresses cached data to reduce storage requirements and network bandwidth usage. |
|
metrics
Package metrics provides an interface for collecting HTTP cache metrics.
|
Package metrics provides an interface for collecting HTTP cache metrics. |
|
metrics/prometheus
Package prometheus provides Prometheus metrics implementation for httpcache.
|
Package prometheus provides Prometheus metrics implementation for httpcache. |
|
multicache
Package multicache provides a multi-tiered cache implementation that allows cascading through multiple cache backends with automatic fallback and promotion.
|
Package multicache provides a multi-tiered cache implementation that allows cascading through multiple cache backends with automatic fallback and promotion. |
|
securecache
Package securecache provides a security wrapper for httpcache.Cache implementations.
|
Package securecache provides a security wrapper for httpcache.Cache implementations. |