Documentation
¶
Overview ¶
Example (MakeKey) ¶
u, err := url.Parse(
"https://example.com:8443/abc?query=param&another=value#fragment=part1&part2",
)
if err != nil {
fmt.Println("Error parsing URL:", err)
return
}
cacheKey := makeKey(u)
fmt.Println("Cache Key:", cacheKey)
Output: Cache Key: https://example.com:8443/abc?query=param&another=value
Index ¶
- Constants
- func NewCacheInvalidator(cache ResponseCache, cke CacheKeyer) *cacheInvalidator
- func NewClock() *clock
- func NewFreshnessCalculator(clock Clock) *freshnessCalculator
- func NewResponseCache(cache Cache) *responseCache
- func NewStaleIfErrorPolicy(clock Clock) *staleIfErrorPolicy
- func NewValidationResponseHandler(clock Clock, ci CacheInvalidator, ce CacheabilityEvaluator, ...) *validationResponseHandler
- func ParseQuotedString(s string) string
- func ParseQuotedStringE(s string) (string, error)
- func SetAgeHeader(resp *http.Response, clock Clock, age *Age)
- func TrimmedCSVSeq(s string) iter.Seq[string]
- type Age
- type CCRequestDirectives
- func (d CCRequestDirectives) MaxAge() (dur time.Duration, valid bool)
- func (d CCRequestDirectives) MaxStale() (dur RawDeltaSeconds, valid bool)
- func (d CCRequestDirectives) MinFresh() (dur time.Duration, valid bool)
- func (d CCRequestDirectives) NoCache() bool
- func (d CCRequestDirectives) NoStore() bool
- func (d CCRequestDirectives) OnlyIfCached() bool
- func (d CCRequestDirectives) StaleIfError() (dur time.Duration, valid bool)
- type CCResponseDirectives
- func (d CCResponseDirectives) MaxAge() (dur time.Duration, valid bool)
- func (d CCResponseDirectives) MaxAgePresent() bool
- func (d CCResponseDirectives) MustRevalidate() bool
- func (d CCResponseDirectives) MustUnderstand() bool
- func (d CCResponseDirectives) NoCache() (fields RawCSVSeq, present bool)
- func (d CCResponseDirectives) NoStore() bool
- func (d CCResponseDirectives) Public() bool
- func (d CCResponseDirectives) StaleIfError() (dur time.Duration, valid bool)
- func (d CCResponseDirectives) StaleWhileRevalidate() (dur time.Duration, valid bool)
- type Cache
- type CacheInvalidator
- type CacheKeyer
- type CacheKeyerFunc
- type CacheStatus
- type CacheabilityEvaluator
- type CacheabilityEvaluatorFunc
- type Clock
- type Entry
- type Freshness
- type FreshnessCalculator
- type HeaderKey
- type MockCache
- type MockCacheInvalidator
- type MockCacheKeyer
- type MockClock
- type MockFreshnessCalculator
- type MockRequestMethodChecker
- type MockResponseCache
- type MockResponseStorer
- type MockRoundTripper
- type MockStaleIfErrorPolicy
- type MockValidationResponseHandler
- type MockVaryMatcher
- type RawCSVSeq
- type RawDeltaSeconds
- type RawTime
- type RequestMethodChecker
- type RequestMethodCheckerFunc
- type ResponseCache
- type ResponseStorer
- type RevalidationContext
- type StaleIfErrorPolicy
- type StaleIfErrorer
- type ValidationResponseHandler
- type VaryMatcher
- type VaryMatcherFunc
Examples ¶
Constants ¶
const CacheStatusHeader = "X-Httpcache-Status"
Variables ¶
This section is empty.
Functions ¶
func NewCacheInvalidator ¶
func NewCacheInvalidator(cache ResponseCache, cke CacheKeyer) *cacheInvalidator
func NewFreshnessCalculator ¶
func NewFreshnessCalculator(clock Clock) *freshnessCalculator
func NewResponseCache ¶
func NewResponseCache(cache Cache) *responseCache
func NewStaleIfErrorPolicy ¶
func NewStaleIfErrorPolicy(clock Clock) *staleIfErrorPolicy
func NewValidationResponseHandler ¶
func NewValidationResponseHandler( clock Clock, ci CacheInvalidator, ce CacheabilityEvaluator, siep StaleIfErrorPolicy, rs ResponseStorer, ) *validationResponseHandler
func ParseQuotedString ¶
ParseQuotedString parses an HTTP quoted-string (RFC 9110 §5.6.4). It returns the unescaped string, or the original string if parsing fails.
func ParseQuotedStringE ¶
ParseQuotedStringE parses an HTTP quoted-string (RFC 9110 §5.6.4). It returns the unescaped string, or an error if the input is not valid.
func SetAgeHeader ¶
SetAgeHeader sets the Age header in the response based on the Age value. It assumes a non-nil Age pointer is provided.
Types ¶
type CCRequestDirectives ¶
CCRequestDirectives is a map of request directives from the Cache-Control header field as defined in RFC 9111, §5.2.1. The keys are the directive tokens, and the values are the arguments (if any) as strings.
This implementation does not perform any transformations on the request, hence we ignore the "no-transform" directive.
func ParseCCRequestDirectives ¶
func ParseCCRequestDirectives(header http.Header) CCRequestDirectives
func (CCRequestDirectives) MaxAge ¶
func (d CCRequestDirectives) MaxAge() (dur time.Duration, valid bool)
MaxAge parses the "max-age" request directive as defined in RFC 9111, §5.2.1.1.
func (CCRequestDirectives) MaxStale ¶
func (d CCRequestDirectives) MaxStale() (dur RawDeltaSeconds, valid bool)
MaxStale parses the "max-stale" request directive as defined in RFC 9111, §5.2.1.2.
func (CCRequestDirectives) MinFresh ¶
func (d CCRequestDirectives) MinFresh() (dur time.Duration, valid bool)
MinFresh parses the "min-fresh" request directive as defined in RFC 9111, §5.2.1.3.
func (CCRequestDirectives) NoCache ¶
func (d CCRequestDirectives) NoCache() bool
NoCache reports the presence of the "no-cache" request directive as defined in RFC 9111, §5.2.1.4.
func (CCRequestDirectives) NoStore ¶
func (d CCRequestDirectives) NoStore() bool
NoStore reports the presence of the "no-store" request directive as defined in RFC 9111, §5.2.1.5.
func (CCRequestDirectives) OnlyIfCached ¶
func (d CCRequestDirectives) OnlyIfCached() bool
OnlyIfCached reports the presence of the "only-if-cached" request directive as defined in RFC 9111, §5.2.1.7.
func (CCRequestDirectives) StaleIfError ¶
func (d CCRequestDirectives) StaleIfError() (dur time.Duration, valid bool)
StaleIfError parses the "stale-if-error" request directive (extension) as defined in RFC 5861, §4.
type CCResponseDirectives ¶
CCResponseDirectives is a map of response directives from the Cache-Control header field. The keys are the directive tokens, and the values are the arguments (if any) as strings.
The following directives per RFC 9111, §5.2.2 are not applicable to private caches:
- "private" (§5.2.2.7)
- "proxy-revalidate" (§5.2.2.8)
- "s-maxage" (§5.2.2.10)
This implementation does not perform any transformations on the request, hence we ignore the "no-transform" directive.
func ParseCCResponseDirectives ¶
func ParseCCResponseDirectives(header http.Header) CCResponseDirectives
func (CCResponseDirectives) MaxAge ¶
func (d CCResponseDirectives) MaxAge() (dur time.Duration, valid bool)
MaxAge parses the "max-age" response directive as defined in RFC 9111, §5.2.2.1.
func (CCResponseDirectives) MaxAgePresent ¶
func (d CCResponseDirectives) MaxAgePresent() bool
MaxAgePresent reports the presence of the "max-age" response directive as defined in RFC 9111, §5.2.2.1.
func (CCResponseDirectives) MustRevalidate ¶
func (d CCResponseDirectives) MustRevalidate() bool
MustRevalidate reports the presence of the "must-revalidate" response directive as defined in RFC 9111, §5.2.2.2.
func (CCResponseDirectives) MustUnderstand ¶
func (d CCResponseDirectives) MustUnderstand() bool
MustUnderstand reports the presence of the "must-understand" response directive as defined in RFC 9111, §5.2.2.3.
func (CCResponseDirectives) NoCache ¶
func (d CCResponseDirectives) NoCache() (fields RawCSVSeq, present bool)
NoCache parses the "no-cache" response directive as defined in RFC 9111, §5.2.2.4.
func (CCResponseDirectives) NoStore ¶
func (d CCResponseDirectives) NoStore() bool
NoStore reports the presence of the "no-store" response directive as defined in RFC 9111, §5.2.2.5.
func (CCResponseDirectives) Public ¶
func (d CCResponseDirectives) Public() bool
Public reports the presence of the "public" response directive as defined in RFC 9111, §5.2.2.9.
func (CCResponseDirectives) StaleIfError ¶
func (d CCResponseDirectives) StaleIfError() (dur time.Duration, valid bool)
StaleIfError parses the "stale-if-error" response directive (extension) as defined in RFC 5861, §4.
func (CCResponseDirectives) StaleWhileRevalidate ¶
func (d CCResponseDirectives) StaleWhileRevalidate() (dur time.Duration, valid bool)
StaleWhileRevalidate parses the "stale-while-revalidate" response directive (extension) as defined in RFC 5861, §3.
type CacheInvalidator ¶
type CacheInvalidator interface {
InvalidateCache(reqURL *url.URL, respHeader http.Header, key string)
}
CacheInvalidator describes the interface implemented by types that can invalidate cache entries based on request and response headers, as specified in RFC 9111 §4.4.
type CacheKeyer ¶
CacheKeyer describes the interface implemented by types that can generate a cache key for a given URL according to RFC 9111 §4.1.
func NewCacheKeyer ¶
func NewCacheKeyer() CacheKeyer
type CacheKeyerFunc ¶
type CacheStatus ¶
type CacheStatus string
const ( CacheStatusHit CacheStatus = "HIT" // Response was served from cache CacheStatusMiss CacheStatus = "MISS" // Response was not found in cache, and was served from origin CacheStatusStale CacheStatus = "STALE" // Response was served from cache but is stale CacheStatusRevalidated CacheStatus = "REVALIDATED" // Response was revalidated with the origin server CacheStatusBypass CacheStatus = "BYPASS" // Response was not served from cache due to cache bypass )
func (CacheStatus) ApplyTo ¶
func (s CacheStatus) ApplyTo(header http.Header)
ApplyTo sets the cache status header on the provided HTTP header.
func (CacheStatus) String ¶
func (s CacheStatus) String() string
type CacheabilityEvaluator ¶
type CacheabilityEvaluator interface {
CanStoreResponse(
resp *http.Response,
reqCC CCRequestDirectives,
resCC CCResponseDirectives,
) bool
}
func NewCacheabilityEvaluator ¶
func NewCacheabilityEvaluator() CacheabilityEvaluator
type CacheabilityEvaluatorFunc ¶
type CacheabilityEvaluatorFunc func( resp *http.Response, reqCC CCRequestDirectives, resCC CCResponseDirectives, ) bool
func (CacheabilityEvaluatorFunc) CanStoreResponse ¶
func (f CacheabilityEvaluatorFunc) CanStoreResponse( resp *http.Response, reqCC CCRequestDirectives, resCC CCResponseDirectives, ) bool
type Entry ¶
type Entry struct {
Response *http.Response // The HTTP response to cache
ReqTime time.Time // Timestamp of the request
RespTime time.Time // Timestamp of the response
}
Entry represents a cached HTTP response entry.
func (*Entry) MarshalBinary ¶
type Freshness ¶
type Freshness struct {
IsStale bool // Whether the response is stale
Age *Age // Current age (seconds) of the response (RFC9111 §4.2.3)
UsefulLife time.Duration // Freshness lifetime (seconds) of the response (RFC9111 §4.2.1)
}
Freshness represents the freshness status of a cached response.
type FreshnessCalculator ¶
type FreshnessCalculator interface {
// CalculateFreshness calculates the freshness of a cached response
// based on the request and response cache control directives.
CalculateFreshness(
resp *Entry,
reqCC CCRequestDirectives,
resCC CCResponseDirectives,
) *Freshness
}
FreshnessCalculator describes the interface implemented by types that can calculate the freshness of a cached response based on request and response cache control directives according to RFC 9111 §4.2.
type MockCache ¶
type MockCacheInvalidator ¶
type MockCacheInvalidator struct {
InvalidateCacheFunc func(reqURL *url.URL, respHeader http.Header, key string)
}
func (*MockCacheInvalidator) InvalidateCache ¶
type MockCacheKeyer ¶
type MockFreshnessCalculator ¶
type MockFreshnessCalculator struct {
CalculateFreshnessFunc func(resp *http.Response, reqCC CCRequestDirectives, resCC CCResponseDirectives) *Freshness
}
func (*MockFreshnessCalculator) CalculateFreshness ¶
func (m *MockFreshnessCalculator) CalculateFreshness( resp *Entry, reqCC CCRequestDirectives, resCC CCResponseDirectives, ) *Freshness
type MockRequestMethodChecker ¶
func (*MockRequestMethodChecker) IsRequestMethodUnderstood ¶
func (m *MockRequestMethodChecker) IsRequestMethodUnderstood(req *http.Request) bool
type MockResponseCache ¶
type MockResponseCache struct {
GetFunc func(key string, req *http.Request) (*Entry, error)
SetFunc func(key string, entry *Entry) error
DeleteFunc func(key string) error
}
func (*MockResponseCache) Delete ¶
func (m *MockResponseCache) Delete(key string) error
type MockResponseStorer ¶
type MockResponseStorer struct {
StoreResponseFunc func(resp *http.Response, key string, reqTime, respTime time.Time) error
}
func (*MockResponseStorer) StoreResponse ¶
type MockRoundTripper ¶
type MockStaleIfErrorPolicy ¶
type MockStaleIfErrorPolicy struct {
CanStaleOnErrorFunc func(freshness *Freshness, sies ...StaleIfErrorer) bool
}
func (*MockStaleIfErrorPolicy) CanStaleOnError ¶
func (m *MockStaleIfErrorPolicy) CanStaleOnError( freshness *Freshness, sies ...StaleIfErrorer, ) bool
type MockValidationResponseHandler ¶
type MockValidationResponseHandler struct {
HandleValidationResponseFunc func(ctx RevalidationContext, req *http.Request, resp *http.Response, err error) (*http.Response, error)
}
func (*MockValidationResponseHandler) HandleValidationResponse ¶
func (m *MockValidationResponseHandler) HandleValidationResponse( ctx RevalidationContext, req *http.Request, resp *http.Response, err error, ) (*http.Response, error)
type MockVaryMatcher ¶
func (*MockVaryMatcher) VaryHeadersMatch ¶
func (m *MockVaryMatcher) VaryHeadersMatch(cachedHdr, reqHdr http.Header) bool
type RawCSVSeq ¶
type RawCSVSeq string
RawCSVSeq is a string that represents a sequence of comma-separated values.
type RawDeltaSeconds ¶
type RawDeltaSeconds string
RawDeltaSeconds is a string that represents a delta time in seconds, as defined in §1.2.2 of RFC 9111.
This implementation supports values up to the maximum range of int64 (9223372036854775807 seconds). Values exceeding 2147483648 (2^31) are valid and will not be capped, as allowed by the RFC, which permits using the greatest positive integer the implementation can represent.
type RequestMethodChecker ¶
RequestMethodChecker describes the interface implemented by types that can check whether the request method is understood by the cache according to RFC 9111 §3.
func NewRequestMethodChecker ¶
func NewRequestMethodChecker() RequestMethodChecker
type RequestMethodCheckerFunc ¶
func (RequestMethodCheckerFunc) IsRequestMethodUnderstood ¶
func (f RequestMethodCheckerFunc) IsRequestMethodUnderstood(req *http.Request) bool
type ResponseCache ¶
type ResponseStorer ¶
type ResponseStorer interface {
StoreResponse(resp *http.Response, key string, reqTime, respTime time.Time) error
}
ResponseStorer describes the interface implemented by types that can store HTTP responses in a cache, as specified in RFC 9111 §3.1.
func NewResponseStorer ¶
func NewResponseStorer(cache ResponseCache) ResponseStorer
NewResponseStorer creates a new ResponseStorer with the given cache.
type RevalidationContext ¶
type StaleIfErrorPolicy ¶
type StaleIfErrorPolicy interface {
CanStaleOnError(freshness *Freshness, sies ...StaleIfErrorer) bool
}
StaleIfErrorPolicy describes the interface implemented by types that can evaluate cache control directives for storing responses (RFC 9111 §3) and determining whether a stale response can be served in case of an error (RFC 5861 §4).
type StaleIfErrorer ¶
type VaryMatcher ¶
VaryMatcher describes the interface implemented by types that can check whether the Vary headers of a cached response match the request headers according to RFC 9111 §4.1.
func NewVaryMatcher ¶
func NewVaryMatcher() VaryMatcher
type VaryMatcherFunc ¶
func (VaryMatcherFunc) VaryHeadersMatch ¶
func (f VaryMatcherFunc) VaryHeadersMatch(cachedHdr, reqHdr http.Header) bool