cache

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxSoftExpirationTTL time.Duration = 10 * time.Second // TODO: Make it customizable?

DefaultMaxSoftExpirationTTL - Additional time to avoid cache stampede (max upper bound).

View Source
const DefaultMinSoftExpirationTTL time.Duration = 5 * time.Second // TODO: Make it customizable?

DefaultMinSoftExpirationTTL - Additional time to avoid cache stampede (min lower bound).

View Source
const FreshSuffix = "/fresh"

FreshSuffix - Used for saving a suffix for handling cache stampede.

View Source
const PurgeAllPath = "/*"

PurgeAllPath - The PURGE request-target that invalidates every cached response for the request's host instead of a single URL.

Variables

View Source
var ErrEmptyValue = errors.New("empty value")

ErrEmptyValue - Error used when no data is available in Redis.

Functions

func FetchMetadata

func FetchMetadata(domainID string, method string, url url.URL) ([]string, error)

FetchMetadata - Returns the cache metadata for the requested URL.

func GetVary

func GetVary(headers http.Header) ([]string, error)

GetVary - Returns the content from the Vary HTTP header.

func PurgeMetadata

func PurgeMetadata(ctx context.Context, domainID string, url url.URL) error

PurgeMetadata - Purges the cache metadata for the requested URL.

func StorageKey

func StorageKey(currentURIObject URIObj, meta []string) string

StorageKey - Returns the cache key for the requested URL.

func StoreMetadata

func StoreMetadata(ctx context.Context, domainID string, method string, url url.URL, meta []string, expiration time.Duration) (bool, error)

StoreMetadata - Saves the cache metadata for the requested URL.

Types

type Object added in v1.3.1

type Object struct {
	ReqID            string
	AllowedStatuses  []int
	AllowedMethods   []string
	CurrentURIObject URIObj
	DomainID         string
}

Object - Contains cache settings and current cached/cacheable object.

func (Object) IsEmptyBodyAllowed added in v1.3.1

func (c Object) IsEmptyBodyAllowed() bool

IsEmptyBodyAllowed - Checks if an empty body is allowed to be cached.

func (Object) IsMethodAllowed added in v1.3.1

func (c Object) IsMethodAllowed() bool

IsMethodAllowed - Checks if a HTTP method is allowed to be cached.

func (Object) IsStatusAllowed added in v1.3.1

func (c Object) IsStatusAllowed() bool

IsStatusAllowed - Checks if a status code is allowed to be cached.

func (Object) IsValid added in v1.3.1

func (c Object) IsValid() (bool, error)

IsValid - Verifies the validity of a cacheable object.

func (Object) PurgeFullPage added in v1.3.1

func (c Object) PurgeFullPage(ctx context.Context) (bool, error)

PurgeFullPage - Deletes the whole page response from cache.

func (Object) PurgeHost added in v1.5.0

func (c Object) PurgeHost(ctx context.Context) (bool, error)

PurgeHost - Deletes every cached response (data and metadata) for the host and scheme of the current URL, over every cacheable method.

Only the request's own scheme: http and https can be separate domains, with their own Redis connection and allowed methods, and this Object carries only the one the PURGE matched.

The method and scheme are spelled out rather than globbed: a "*" there also matches across the "@@" separator, so "DATA@@*@@http://host/*" would match another host's key whose path happens to contain "@@http://host/".

func (*Object) RetrieveFullPage added in v1.3.1

func (c *Object) RetrieveFullPage() error

RetrieveFullPage - Retrieves the whole page response from cache.

func (Object) StoreFullPage added in v1.3.1

func (c Object) StoreFullPage(ctx context.Context, expiration time.Duration) (bool, error)

StoreFullPage - Stores the whole page response in cache.

type URIObj

type URIObj struct {
	URL             url.URL
	Method          string
	StatusCode      int
	RequestHeaders  http.Header
	ResponseHeaders http.Header
	Content         [][]byte
	Stale           bool
}

URIObj - Holds details about the response.

func (URIObj) GetHeadersChecksum added in v1.3.1

func (u URIObj) GetHeadersChecksum(meta []string) string

GetHeadersChecksum - Returns a SHA256 based on the HTTP Request Headers.

func (URIObj) HasRangeRequest added in v1.3.1

func (u URIObj) HasRangeRequest() bool

HasRangeRequest - Checks if the incoming request asked for a byte range. Range requests must always bypass the cache: this cache stores/serves whole bodies, so returning a full cached body for a ranged request would be a wrong response (wrong status code and bytes) to the client.

func (URIObj) IsPartialContent added in v1.3.1

func (u URIObj) IsPartialContent() bool

IsPartialContent - Checks if the response is a partial content response (206, or carrying Content-Range). Such responses must never be stored as if they were the full resource, or later non-range requests would be served truncated bytes.

func (URIObj) IsStorableBySharedCache added in v1.3.3

func (u URIObj) IsStorableBySharedCache() bool

IsStorableBySharedCache - Checks whether a shared cache may store the response at all (RFC 9111 §3), whatever TTL it would otherwise get (this also covers the negative TTL override):

  • no-store, private (incl. private="...") and no-cache forbid it, see ttl.ForbidsStoring;
  • a response to a request carrying Authorization is only storable when the response explicitly allows it with public, s-maxage or must-revalidate (RFC 9111 §3.5).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL