Documentation ¶
Overview ¶
microcache is a non-standard HTTP microcache implemented as Go middleware.
Index ¶
- type Compressor
- type CompressorGzip
- type CompressorSnappy
- type Config
- type Driver
- type DriverARC
- func (c DriverARC) Get(hash string) (res Response)
- func (c DriverARC) GetRequestOpts(hash string) (req RequestOpts)
- func (c DriverARC) GetSize() int
- func (c DriverARC) Remove(hash string) error
- func (c DriverARC) Set(hash string, res Response) error
- func (c DriverARC) SetRequestOpts(hash string, req RequestOpts) error
- type DriverLRU
- func (c DriverLRU) Get(hash string) (res Response)
- func (c DriverLRU) GetRequestOpts(hash string) (req RequestOpts)
- func (c DriverLRU) GetSize() int
- func (c DriverLRU) Remove(hash string) error
- func (c DriverLRU) Set(hash string, res Response) error
- func (c DriverLRU) SetRequestOpts(hash string, req RequestOpts) error
- type Microcache
- type Monitor
- type RequestOpts
- type Response
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Compressor ¶
type Compressor interface { // Compress compresses a response prior to being saved in the cache and returns a clone // usually by compressing the response body Compress(Response) Response // Expand decompresses a response's body (destructively) Expand(Response) Response }
Compressor is the interface for response compressors
type CompressorGzip ¶
type CompressorGzip struct { }
CompressorGzip is a gzip compressor
func (CompressorGzip) Compress ¶
func (c CompressorGzip) Compress(res Response) Response
func (CompressorGzip) Expand ¶
func (c CompressorGzip) Expand(res Response) Response
type CompressorSnappy ¶
type CompressorSnappy struct { }
CompressorSnappy is a Snappy compressor 14x faster compress than gzip 8x faster expand than gzip ~ 1.5 - 2x larger result (see README)
func (CompressorSnappy) Compress ¶
func (c CompressorSnappy) Compress(res Response) Response
func (CompressorSnappy) Expand ¶
func (c CompressorSnappy) Expand(res Response) Response
type Config ¶
type Config struct { // Nocache prevents responses from being cached by default // Can be overridden by the microcache-cache and microcache-nocache response headers Nocache bool // Timeout specifies the maximum execution time for backend responses // Example: If the underlying handler takes more than 10s to respond, // the request is cancelled and the response is treated as 503 // Recommended: 10s // Default: 0 Timeout time.Duration // TTL specifies a default ttl for cached responses // Can be overridden by the microcache-ttl response header // Recommended: 10s // Default: 0 TTL time.Duration // StaleWhileRevalidate specifies a period during which a stale response may be // served immediately while the resource is fetched in the background. This can be // useful for ensuring consistent response times at the cost of content freshness. // More Info: https://tools.ietf.org/html/rfc5861 // Recommended: 20s // Default: 0 StaleWhileRevalidate time.Duration // StaleIfError specifies a default stale grace period // If a request fails and StaleIfError is set, the object will be served as stale // and the response will be re-cached for the duration of this grace period // Can be overridden by the microcache-ttl-stale response header // More Info: https://tools.ietf.org/html/rfc5861 // Recommended: 20s // Default: 0 StaleIfError time.Duration // StaleRecache specifies whether to re-cache the response object for ttl while serving // stale response on backend error // Recommended: true // Default: false StaleRecache bool // CollapsedForwarding specifies whether to collapse duplicate requests // This helps prevent servers with a cold cache from hammering the backend // Default: false CollapsedForwarding bool // HashQuery determines whether all query parameters in the request URI // should be hashed to differentiate requests // Default: false HashQuery bool // QueryIgnore is a list of query parameters to ignore when hashing // Default: nil QueryIgnore []string // Vary specifies a list of http request headers by which all requests // should be differentiated. When making use of this option, it may be a good idea // to normalize these headers first using a separate piece of middleware. // // []string{"accept-language", "accept-encoding", "xml-http-request"} // // Default: []string{} Vary []string // Driver specifies a cache storage driver // Default: lru with 10,000 item capacity Driver Driver // Compressor specifies a compressor to use for reducing the memory required to cache // response bodies // Default: nil Compressor Compressor // Monitor is an optional parameter which will periodically report statistics about // the cache to enable monitoring of cache size, cache efficiency and error rate // Default: nil Monitor Monitor // Exposed determines whether to add a header to the response indicating the response state // Microcache: ( HIT | MISS | STALE ) // Default: false Exposed bool // SuppressAgeHeader determines whether to suppress the age header in responses // The age header is added by default to all HIT and STALE responses // Age: ( seconds ) // Default: false SuppressAgeHeader bool }
type Driver ¶
type Driver interface { // SetRequestOpts stores request options in the request cache. // Requests contain request-specific cache configuration based on response headers SetRequestOpts(string, RequestOpts) error // GetRequestOpts retrieves request options from the request cache GetRequestOpts(string) RequestOpts // Set stores a response object in the response cache. // This contains the full response as well as an expiration date. Set(string, Response) error // Get retrieves a response object from the response cache Get(string) Response // Remove removes a response object from the response cache. // Required by HTTP spec to purge cached responses after successful unsafe request. Remove(string) error // GetSize returns the number of objects stored in the cache GetSize() int }
Driver is the interface for cache drivers
type DriverARC ¶
type DriverARC struct { RequestCache *lru.ARCCache ResponseCache *lru.ARCCache }
DriverARC is a driver implementation using github.com/hashicorp/golang-lru ARCCache is a thread-safe fixed size Adaptive Replacement Cache (ARC). It requires more ram and cpu than straight LRU but can be more efficient https://godoc.org/github.com/hashicorp/golang-lru#ARCCache
func NewDriverARC ¶
NewDriverARC returns an ARC driver. size determines the number of items in the cache. Memory usage should be considered when choosing the appropriate cache size. The amount of memory consumed by the driver will depend upon the response size. Roughly, memory = cacheSize * averageResponseSize / compression ratio ARC caches have additional CPU and memory overhead when compared with LRU ARC does not support eviction monitoring
func (DriverARC) GetRequestOpts ¶
func (c DriverARC) GetRequestOpts(hash string) (req RequestOpts)
func (DriverARC) SetRequestOpts ¶
func (c DriverARC) SetRequestOpts(hash string, req RequestOpts) error
type DriverLRU ¶
type DriverLRU struct { RequestCache *lru.Cache ResponseCache *lru.Cache }
DriverLRU is a driver implementation using github.com/hashicorp/golang-lru
func NewDriverLRU ¶
NewDriverLRU returns the default LRU driver configuration. size determines the number of items in the cache. Memory usage should be considered when choosing the appropriate cache size. The amount of memory consumed by the driver will depend upon the response size. Roughly, memory = cacheSize * averageResponseSize / compression ratio
func (DriverLRU) GetRequestOpts ¶
func (c DriverLRU) GetRequestOpts(hash string) (req RequestOpts)
func (DriverLRU) SetRequestOpts ¶
func (c DriverLRU) SetRequestOpts(hash string, req RequestOpts) error
type Microcache ¶
type Monitor ¶
type Monitor interface { GetInterval() time.Duration Log(Stats) Hit() Miss() Stale() Backend() Error() }
Monitor is an interface for collecting metrics about the microcache
type RequestOpts ¶
type RequestOpts struct {
// contains filtered or unexported fields
}
RequestOpts stores per-request cache options. This is necessary to allow custom response headers to be evaluated, cached and applied prior to response object retrieval (ie. microcache-vary, microcache-nocache, etc)
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is used both as a cache object for the response and to wrap http.ResponseWriter for downstream requests.