Documentation
¶
Overview ¶
Package smartcache provides RFC 7234 compliant HTTP caching middleware. It handles Cache-Control, ETag, If-None-Match, If-Modified-Since, If-None-Match, and conditional request semantics automatically.
The cache supports both in-memory storage and pluggable backends. Responses are stored with their headers and body, and served with proper 304 Not Modified responses when applicable.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultConfig = Config{ Methods: []string{"GET", "HEAD"}, CacheableStatusCodes: []int{200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501}, MaxSize: 10000, DefaultTTL: 5 * time.Minute, MaxBodySize: 1 << 20, }
DefaultConfig returns the default configuration.
Functions ¶
Types ¶
type CacheControl ¶
type CacheControl struct {
MaxAge time.Duration
SMaxAge time.Duration
NoCache bool
NoStore bool
Public bool
Private bool
Immutable bool
MustRevalidate bool
ProxyRevalidate bool
NoTransform bool
StaleIfError time.Duration
}
CacheControl directives parsed from Cache-Control header.
type Config ¶
type Config struct {
// Store is the cache backend. Default: kv.NewMemoryStore sized to
// MaxSize. Pass any kv.Store implementation when constructing the
// middleware; file-backed stores should be constructed with
// kv.WithMaxEntrySize(maxCacheEntrySize) to bound persisted entry size.
Store kv.Store
// Methods is the list of HTTP methods to cache. Default: ["GET"].
Methods []string
// CacheableStatusCodes is the list of status codes to cache. Default: [200].
CacheableStatusCodes []int
// MaxSize is the maximum number of cached responses. Default: 10000.
MaxSize int
// DefaultTTL is the default TTL when Cache-Control is not set. Default: 5m.
DefaultTTL time.Duration
// MaxBodySize is the maximum response body size to cache. Default: 1MB.
MaxBodySize int
// Next is an optional skip function.
Next func(ctx fh.Ctx) bool
// KeyFunc generates a cache key from the request. Default: method + path + query.
KeyFunc func(ctx fh.Ctx) string
// VaryHeaders lists additional request headers folded into the cache
// key (e.g. "Accept-Encoding", "Accept-Language") so a response cached
// for one variant (compressed, localized, ...) is never served to a
// request that asked for a different one.
VaryHeaders []string
// AllowRequestCookies permits caching/serving requests that carry a
// Cookie header. Default false: requests with an Authorization or
// Cookie header are never read from or written to the shared cache,
// since a personalized/authenticated response could otherwise be
// cached and served to a different caller on the same path.
AllowRequestCookies bool
}
Config holds configuration for the smartcache middleware.
Click to show internal directories.
Click to hide internal directories.