Documentation
¶
Index ¶
- func KeyAsString(key uint64) string
- type Adapter
- type AdapterTouch
- type CacheEvent
- type CacheEventType
- type Client
- type ClientOption
- func ClientWithAdapter(a Adapter) ClientOption
- func ClientWithExpiresHeader() ClientOption
- func ClientWithMaxBodySize(size int) ClientOption
- func ClientWithMethods(methods []string) ClientOption
- func ClientWithObserver(observer Observer) ClientOption
- func ClientWithPurge() ClientOption
- func ClientWithRefreshKey(refreshKey string) ClientOption
- func ClientWithRespectCacheControl() ClientOption
- func ClientWithSingleflight() ClientOption
- func ClientWithSkipCacheResponseHeader(header string) ClientOption
- func ClientWithSkipCacheURIPathRegex(pathRegex *regexp.Regexp) ClientOption
- func ClientWithStaleWhileRevalidate(window time.Duration) ClientOption
- func ClientWithStatusCodeFilter(filter func(int) bool) ClientOption
- func ClientWithTTL(ttl time.Duration) ClientOption
- func ClientWithVaryHeaders(headers []string) ClientOption
- type Observer
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func KeyAsString ¶
KeyAsString can be used by adapters to convert the cache key from uint64 to string.
Types ¶
type Adapter ¶
type Adapter interface {
// Get retrieves the cached response by a given key. It also
// returns true or false, whether it exists or not.
Get(key uint64) ([]byte, bool)
// Set caches a response for a given key until an expiration date.
Set(key uint64, response []byte, expiration time.Time)
// Release frees cache for a given key.
Release(key uint64)
}
Adapter interface for HTTP cache middleware client.
type AdapterTouch ¶
type AdapterTouch interface {
Touch(key uint64)
}
AdapterTouch is an optional Adapter extension. When an adapter implements it, the middleware records each cache hit via Touch instead of re-encoding the response and calling Set, eliminating the read-modify-write lost-update race and the per-hit gob round-trip. Adapters that do not implement Touch keep receiving the legacy Set call so existing custom adapters retain their behavior.
type CacheEvent ¶
type CacheEvent struct {
Type CacheEventType
Request *http.Request
Key uint64
StatusCode int
}
CacheEvent is passed to an observer when cache middleware events happen.
type CacheEventType ¶
type CacheEventType string
CacheEventType identifies an observed cache middleware event.
const ( // CacheEventHit means a valid cached response was served. CacheEventHit CacheEventType = "hit" // CacheEventMiss means no cached response was found. CacheEventMiss CacheEventType = "miss" // CacheEventStale means an expired cached response was found and released. CacheEventStale CacheEventType = "stale" // CacheEventRefresh means a cached response was explicitly released. CacheEventRefresh CacheEventType = "refresh" // CacheEventStore means a response was stored in cache. CacheEventStore CacheEventType = "store" // CacheEventPurge means a cached response was explicitly purged. CacheEventPurge CacheEventType = "purge" )
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client data structure for HTTP cache middleware.
func NewClient ¶
func NewClient(opts ...ClientOption) (*Client, error)
NewClient initializes the cache HTTP middleware client with the given options.
type ClientOption ¶
ClientOption is used to set Client settings.
func ClientWithAdapter ¶
func ClientWithAdapter(a Adapter) ClientOption
ClientWithAdapter sets the adapter type for the HTTP cache middleware client.
func ClientWithExpiresHeader ¶
func ClientWithExpiresHeader() ClientOption
ClientWithExpiresHeader enables middleware to add an Expires header to responses. Optional setting. If not set, default is false.
func ClientWithMaxBodySize ¶
func ClientWithMaxBodySize(size int) ClientOption
ClientWithMaxBodySize caps the response body bytes the middleware will buffer and cache. Responses that grow past the limit are still streamed to the client untouched, but their buffered copy is dropped and the entry is not stored. Defaults to no limit (0) for backward compatibility; that default lets a single oversized response (a download, an unbounded stream) hold its bytes in memory until the handler returns, so set a cap for any endpoint that can emit large payloads.
func ClientWithMethods ¶
func ClientWithMethods(methods []string) ClientOption
ClientWithMethods sets the acceptable HTTP methods to be cached. Optional setting. If not set, default is "GET".
func ClientWithObserver ¶
func ClientWithObserver(observer Observer) ClientOption
ClientWithObserver sets a function that receives cache middleware events. Optional setting.
func ClientWithPurge ¶
func ClientWithPurge() ClientOption
ClientWithPurge enables handling PURGE requests by releasing matching cache entries. Optional setting.
func ClientWithRefreshKey ¶
func ClientWithRefreshKey(refreshKey string) ClientOption
ClientWithRefreshKey sets the parameter key used to free a request cached response. Optional setting.
func ClientWithRespectCacheControl ¶
func ClientWithRespectCacheControl() ClientOption
ClientWithRespectCacheControl makes the middleware honor a small but useful subset of RFC 7234 Cache-Control directives on both requests and responses:
- Request Cache-Control: no-store -> bypass the cache entirely (no lookup, no store).
- Request Cache-Control: no-cache -> skip the cache lookup so the handler always runs, but still store the response.
- Response Cache-Control: no-store, no-cache, or private -> do not store the response.
- Response Cache-Control: s-maxage=N / max-age=N -> override the client's default TTL for this response (s-maxage wins, matching shared-cache semantics).
Defaults off so existing applications that emit Cache-Control purely for downstream clients see no behavior change.
func ClientWithSingleflight ¶
func ClientWithSingleflight() ClientOption
ClientWithSingleflight coalesces concurrent cache misses for the same key so the origin handler runs only once per cache-miss batch. All concurrent callers receive the same response. Defaults off so callers who depend on per-request handler invocations are not surprised.
func ClientWithSkipCacheResponseHeader ¶
func ClientWithSkipCacheResponseHeader(header string) ClientOption
ClientWithSkipCacheResponseHeader sets a response header that prevents successful responses from being stored.
func ClientWithSkipCacheURIPathRegex ¶
func ClientWithSkipCacheURIPathRegex(pathRegex *regexp.Regexp) ClientOption
ClientWithSkipCacheURIPathRegex skips cache lookup and storage for matching request URL paths.
func ClientWithStaleWhileRevalidate ¶
func ClientWithStaleWhileRevalidate(window time.Duration) ClientOption
ClientWithStaleWhileRevalidate enables RFC 5861 stale-while-revalidate semantics: an expired entry whose Expiration is no older than window is served from cache immediately while a single background goroutine refreshes it from the origin. Concurrent stale hits coalesce so only one refresh runs per key at a time. Defaults to 0 (off), in which case expired entries continue to fall through to the existing release-and-miss path.
func ClientWithStatusCodeFilter ¶
func ClientWithStatusCodeFilter(filter func(int) bool) ClientOption
ClientWithStatusCodeFilter sets the response status codes that can be cached. Optional setting. If not set, responses below 400 are cached.
func ClientWithTTL ¶
func ClientWithTTL(ttl time.Duration) ClientOption
ClientWithTTL sets how long each response is going to be cached.
func ClientWithVaryHeaders ¶
func ClientWithVaryHeaders(headers []string) ClientOption
ClientWithVaryHeaders includes selected request headers in cache keys.
type Response ¶
type Response struct {
// Value is the cached response value.
Value []byte
// Header is the cached response header.
Header http.Header
// Expiration is the cached response expiration date.
Expiration time.Time
// LastAccess is the last date a cached response was accessed.
// Used by LRU and MRU algorithms.
LastAccess time.Time
// Frequency is the count of times a cached response is accessed.
// Used for LFU and MFU algorithms.
Frequency int
// CanonicalKey is a fingerprint of the request inputs (URL, vary
// headers and body) that produced this entry. The middleware
// compares it against the incoming request on every hit so an
// FNV-64 collision (or a manually corrupted entry) cannot serve
// one client's response to another. Entries written by older
// versions of this package have an empty CanonicalKey and bypass
// verification for backward compatibility.
CanonicalKey []byte
}
Response is the cached response data structure.
func BytesToResponse ¶
BytesToResponse converts bytes array into Response data structure. Decoding errors are silently swallowed for backward compatibility; the middleware uses decodeResponse internally so it can detect corruption and treat the entry as a cache miss.