caching

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 35 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 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 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