cache

package
v0.0.1-alpha88 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package cache provides HTTP response caching with LRU eviction.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheMiss indicates the requested item is not in cache.
	ErrCacheMiss = errors.New("cache miss")
	// ErrCacheDisabled indicates the cache is not enabled.
	ErrCacheDisabled = errors.New("cache disabled")
)

Functions

func RestoreResponse

func RestoreResponse(entry *Entry, req *http.Request) *http.Response

RestoreResponse creates an HTTP response from a cache entry.

Types

type Cache

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

Cache provides content caching with LRU eviction.

func New

func New(dir string, maxSize int64, enabled bool, logger *zap.Logger) (*Cache, error)

New creates a new cache instance.

func (*Cache) BeginStreamingPut

func (c *Cache) BeginStreamingPut(key string, resp *http.Response) (*StreamingPut, error)

BeginStreamingPut creates a cache writer that can persist a response body as it streams through the proxy.

func (*Cache) Clear

func (c *Cache) Clear() error

Clear removes all cached entries.

func (*Cache) Get

func (c *Cache) Get(key string) (*Entry, error)

Get retrieves a cached response.

func (*Cache) GetStats

func (c *Cache) GetStats() Stats

GetStats returns current cache statistics.

func (*Cache) Put

func (c *Cache) Put(key string, entry *Entry) error

Put stores a response in the cache.

type Entry

type Entry struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
	CachedAt   time.Time
	Size       int64
	// contains filtered or unexported fields
}

Entry represents a cached HTTP response.

func CaptureResponse

func CaptureResponse(resp *http.Response) (*Entry, error)

CaptureResponse captures an HTTP response for caching.

type Matcher

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

Matcher determines if a request should be cached.

func NewMatcher

func NewMatcher(patterns []string, contentAware bool) (*Matcher, error)

NewMatcher creates a new cache matcher.

When contentAware is true, requests are cached when the URL path contains a sha256 digest and the request/response headers indicate Docker/OCI registry content.

Explicit patterns are evaluated alongside content-aware detection — a request is cached when either check passes. This lets callers cover CDN backends (e.g. Cloudflare R2 registry storage) whose URLs don't carry standard OCI Accept headers while still using content-aware detection for normal registry traffic.

func (*Matcher) GenerateKey

func (m *Matcher) GenerateKey(req *http.Request) string

GenerateKey generates a cache key from a request.

func (*Matcher) ShouldCache

func (m *Matcher) ShouldCache(req *http.Request) bool

ShouldCache determines if a request should be cached. Returns true if the content-aware check passes OR any explicit pattern matches.

func (*Matcher) ShouldCacheResponse

func (m *Matcher) ShouldCacheResponse(resp *http.Response) bool

ShouldCacheResponse determines if a response should be cached.

func (*Matcher) VerifyDigest

func (m *Matcher) VerifyDigest(path string, body []byte) error

VerifyDigest checks that body's sha256 hash matches the digest embedded in the URL path. Recognises two formats:

  • OCI standard: "sha256:HEX64" (e.g. /v2/…/blobs/sha256:abc…)
  • Registry storage path: "/HEX64/" (e.g. /registry-v2/…/sha256/ab/HEX64/data)

Returns nil if no digest is found in the path or the digest matches. Returns an error describing the mismatch otherwise.

func (*Matcher) VerifyDigestHex

func (m *Matcher) VerifyDigestHex(path string, actual string) error

VerifyDigestHex checks that actual matches the sha256 digest embedded in the URL path.

type Stats

type Stats struct {
	Hits        int64
	Misses      int64
	Stores      int64
	Evictions   int64
	Errors      int64
	CurrentSize int64
}

Stats tracks cache statistics.

type StreamingPut

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

StreamingPut incrementally persists a response body while it is being streamed to the client.

func (*StreamingPut) Abort

func (s *StreamingPut) Abort() error

Abort discards any partially written cache file.

func (*StreamingPut) Commit

func (s *StreamingPut) Commit() error

Commit atomically moves the streamed response into the cache and updates the in-memory index.

func (*StreamingPut) DigestHex

func (s *StreamingPut) DigestHex() string

DigestHex returns the sha256 digest of the streamed body.

func (*StreamingPut) Size

func (s *StreamingPut) Size() int64

Size returns the streamed response body size.

func (*StreamingPut) Write

func (s *StreamingPut) Write(p []byte) (int, error)

Write appends body bytes to the cache file while updating the digest.

Jump to

Keyboard shortcuts

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