caching

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const BYPASS = "BYPASS"

Variables

View Source
var (
	ErrHeaderNoMatchVaryKey  = errors.New("header no match vary key")
	ErrHeaderNoMatchVaryData = errors.New("header no match vary data")

	// ErrVarySizeLimited indicates the number of Vary versions has exceeded the maximum limit.
	ErrVarySizeLimited = errors.New("vary size exceed limit")

	// ErrVaryDowngradeNormal indicates the cache has been downgraded from Vary to normal cache.
	ErrVaryDowngradeNormal = errors.New("vary downgrade to normal cache")
)

Functions

func Middleware

func Middleware(c *configv1.Middleware) (middleware.Middleware, func(), error)

Middleware initializes a middleware component based on the provided configuration and returns the middleware logic.

Types

type Caching

type Caching struct {
	// contains filtered or unexported fields
}

func (*Caching) Key

func (c *Caching) Key() string

Key implements storage.ResourceLocker.

func (*Caching) Lock

func (c *Caching) Lock()

Lock implements storage.ResourceLocker.

func (*Caching) RLock

func (c *Caching) RLock()

RLock implements storage.ResourceLocker.

func (*Caching) RUnlock

func (c *Caching) RUnlock()

RUnlock implements storage.ResourceLocker.

func (*Caching) Unlock

func (c *Caching) Unlock()

Unlock implements storage.ResourceLocker.

type ChunkFlightGroup added in v1.2.0

type ChunkFlightGroup struct {
	// contains filtered or unexported fields
}

ChunkFlightGroup collapses concurrent upstream requests for different byte ranges of the same object into a single origin fetch covering the union of all requested ranges. Response body bytes are fanned out to all waiters via io.MultiWriter + io.Pipe, and each caller trims the shared stream down to its own sub-range with iobuf.RangeReader.

This mirrors Squid's collapsed_forwarding at the chunk/segment level: when two goroutines request different byte ranges of the same cached object, only one hits origin and the others wait, even when the ranges differ.

func (*ChunkFlightGroup) Do added in v1.2.0

func (g *ChunkFlightGroup) Do(objectKey string, fromByte, toByte uint64, waiter time.Duration, fn func(unionFrom, unionTo uint64) (*http.Response, error)) (io.ReadCloser, bool, error)

Do executes fn once per objectKey, passing the union of all registered byte ranges to fn. All callers — including the first — receive an io.PipeReader trimmed to exactly [fromByte, toByte]. The returned bool reports whether this caller shared an in-flight request.

waiter is the duration the leader goroutine pauses *before* calling fn, giving late-arriving callers a window to register under the same key. In production the network round-trip naturally provides this window; waiter ensures correctness even when fn would otherwise complete nearly instantly (e.g. in tests, or for tiny ranges on a local origin).

Contract: fn owns resp.Body. On success ChunkFlightGroup reads and closes it. On error fn must either return (nil, err) or close the body before returning (resp, err).

type Duration

type Duration string

func (Duration) AsDuration

func (r Duration) AsDuration() time.Duration

type FileChangeOption

type FileChangeOption func(r *FileChangedProcessor)

FileChangeOption represents a functional option for configuring a FileChangedProcessor.

type FileChangedProcessor

type FileChangedProcessor struct {
}

FileChangedProcessor handles the detection and processing of file changes during cache operations.

func (*FileChangedProcessor) Lookup

func (r *FileChangedProcessor) Lookup(_ *Caching, _ *http.Request) (bool, error)

Lookup determines the validity of a file change based on the provided caching mechanism and HTTP request.

func (*FileChangedProcessor) PostRequest

func (r *FileChangedProcessor) PostRequest(c *Caching, req *http.Request, resp *http.Response) (*http.Response, error)

PostRequest processes the HTTP response after it is received, checking for file changes and metadata mismatches. It validates content length, ETag, and Last-Modified headers, and handles discrepancies by updating the cache state.

func (*FileChangedProcessor) PreRequest

func (r *FileChangedProcessor) PreRequest(_ *Caching, req *http.Request) (*http.Request, error)

PreRequest processes the HTTP request before it is sent, potentially modifying it, and returns the updated request.

type FillRangeOption

type FillRangeOption func(f *fillRange)

func WithChunkSize

func WithChunkSize(chunkSize uint64) FillRangeOption

func WithFillRangePercent

func WithFillRangePercent(fillRangePercent int) FillRangeOption

type ObjectFlightGroup added in v1.2.0

type ObjectFlightGroup struct {
	// contains filtered or unexported fields
}

ObjectFlightGroup collapses concurrent full-MISS requests for the same cache object. Unlike ChunkFlightGroup (which works at the chunk/segment level), this operates at the whole-object level — it ensures only one goroutine hits origin for a given cache key.

The returned response carries the headers from the leader's fn and a body that fans out to all concurrent callers. Callers must close the body.

func (*ObjectFlightGroup) Do added in v1.2.0

func (g *ObjectFlightGroup) Do(key string, waiter time.Duration, fn func() (*http.Response, error)) (*http.Response, bool, error)

Do executes fn once per key and fans out the response body to all concurrent callers. All callers receive the same response headers (cloned) and a shared body stream.

waiter is the duration the leader pauses before calling fn, giving late-arriving callers a window to register under the same key.

Returns:

resp  — a response carrying the leader's headers and a shared body
shared — true if this caller joined an existing flight
err    — error from fn or from body copy

type PrefetchOption

type PrefetchOption func(r *PrefetchProcessor)

type PrefetchProcessor

type PrefetchProcessor struct{}

func (*PrefetchProcessor) Lookup

func (r *PrefetchProcessor) Lookup(c *Caching, req *http.Request) (bool, error)

func (*PrefetchProcessor) PostRequest

func (r *PrefetchProcessor) PostRequest(c *Caching, req *http.Request, resp *http.Response) (*http.Response, error)

func (*PrefetchProcessor) PreRequest

func (r *PrefetchProcessor) PreRequest(c *Caching, req *http.Request) (*http.Request, error)

type Processor

type Processor interface {
	// Lookup checks if the request hits the cache.
	Lookup(caching *Caching, req *http.Request) (bool, error)
	// PreRequest processes the request before sending it to the origin server.
	PreRequest(caching *Caching, req *http.Request) (*http.Request, error)
	// PostRequest processes the response received from the origin server.
	PostRequest(caching *Caching, req *http.Request, resp *http.Response) (*http.Response, error)
}

Processor defines the interface for caching processor middleware.

func NewFileChangedProcessor

func NewFileChangedProcessor(opts ...FileChangeOption) Processor

NewFileChangedProcessor creates and returns a new instance of FileChangedProcessor with optional configuration options.

func NewFillRangeProcessor

func NewFillRangeProcessor(opts ...FillRangeOption) Processor

func NewPrefetchProcessor

func NewPrefetchProcessor(opts ...PrefetchOption) Processor

func NewRevalidateProcessor

func NewRevalidateProcessor(opts ...RefreshOption) Processor

func NewStateProcessor

func NewStateProcessor() Processor

type ProcessorChain

type ProcessorChain []Processor

ProcessorChain represents a chain of caching processors.

func NewProcessorChain

func NewProcessorChain(processors ...Processor) *ProcessorChain

NewProcessorChain creates a new ProcessorChain with the given processors.

func (*ProcessorChain) Lookup

func (pc *ProcessorChain) Lookup(caching *Caching, req *http.Request) (bool, error)

Lookup iterates through the processor chain to check for a cache hit.

func (*ProcessorChain) PostRequest

func (pc *ProcessorChain) PostRequest(caching *Caching, req *http.Request, resp *http.Response) (*http.Response, error)

PostRequest processes the response through the processor chain after receiving it from the origin server.

func (*ProcessorChain) PreRequest

func (pc *ProcessorChain) PreRequest(caching *Caching, req *http.Request) (*http.Request, error)

PreRequest processes the request through the processor chain before sending it to the origin server.

func (*ProcessorChain) String

func (pc *ProcessorChain) String() string

String returns a string representation of the processor chain.

type RefreshOption

type RefreshOption func(r *RevalidateProcessor)

type RevalidateProcessor

type RevalidateProcessor struct{}

func (*RevalidateProcessor) Lookup

func (r *RevalidateProcessor) Lookup(c *Caching, req *http.Request) (bool, error)

func (*RevalidateProcessor) PostRequest

func (r *RevalidateProcessor) PostRequest(c *Caching, req *http.Request, resp *http.Response) (*http.Response, error)

func (*RevalidateProcessor) PreRequest

func (r *RevalidateProcessor) PreRequest(c *Caching, req *http.Request) (*http.Request, error)

type StateProcessor

type StateProcessor struct{}

func (*StateProcessor) Lookup

func (s *StateProcessor) Lookup(c *Caching, req *http.Request) (bool, error)

Lookup implements Processor.

func (*StateProcessor) PostRequest

func (s *StateProcessor) PostRequest(c *Caching, req *http.Request, resp *http.Response) (*http.Response, error)

PostRequest implements Processor.

func (*StateProcessor) PreRequest

func (s *StateProcessor) PreRequest(c *Caching, req *http.Request) (*http.Request, error)

PreRequest implements Processor.

type VaryOption

type VaryOption func(r *VaryProcessor)

func WithVaryIgnoreKeys

func WithVaryIgnoreKeys(keys ...string) VaryOption

WithVaryIgnoreKeys specifies header keys to be ignored during Vary processing.

func WithVaryMaxLimit

func WithVaryMaxLimit(limit int) VaryOption

WithVaryMaxLimit sets the maximum number of Vary versions allowed per URL.

type VaryProcessor

type VaryProcessor struct {
	// contains filtered or unexported fields
}

func NewVaryProcessor

func NewVaryProcessor(opts ...VaryOption) *VaryProcessor

NewVaryProcessor creates a new VaryProcessor with the given options. Default configuration:

  • maxLimit: 100 (maximum Vary versions per URL)

func (*VaryProcessor) Lookup

func (v *VaryProcessor) Lookup(caching *Caching, req *http.Request) (bool, error)

Lookup checks if a cached response exists for the given request. It returns true if a matching Vary cache entry is found, false otherwise.

The lookup process:

  1. Check if the request has no-cache directive
  2. Verify if the cached object has Vary index
  3. Find the matching Vary cache based on request headers

func (*VaryProcessor) PostRequest

func (v *VaryProcessor) PostRequest(caching *Caching, req *http.Request, resp *http.Response) (*http.Response, error)

PostRequest processes the response from the origin server and handles Vary caching. It converts the response to a Vary-aware cache structure if the response contains Vary headers.

func (*VaryProcessor) PreRequest

func (v *VaryProcessor) PreRequest(_ *Caching, req *http.Request) (*http.Request, error)

PreRequest performs pre-processing before the request is forwarded to the origin. Currently, this is a no-op for VaryProcessor.

Jump to

Keyboard shortcuts

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