cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cache stores downloaded artifacts and their scan results.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get returns the cached entry for ref, or (nil, false) on miss or expiry.
	Get(ref *gate.PackageRef) (*CacheEntry, bool)
	// Put copies the artifact at tmpPath into the cache store and records the scan result.
	Put(ref *gate.PackageRef, tmpPath string, scanClean bool, scanJSON string) error
	// Invalidate removes the cached entry and its artifact file for ref.
	Invalidate(ref *gate.PackageRef) error
	// Stats returns aggregate cache statistics.
	Stats() (CacheStats, error)
	// Close stops background workers and releases the index.
	Close() error
}

Cache is the storage interface for package artifacts and scan results.

func New

func New(cfg config.CacheConfig) (Cache, error)

New constructs the cache backend selected by cfg.Backend. "" and "local" build a LocalCache; "s3" is reserved but not yet implemented (fail-fast rather than silently falling back to local).

type CacheEntry

type CacheEntry struct {
	// ArtifactPath is the absolute path to the cached file on disk.
	ArtifactPath string
	// ScanClean is true if all scanners passed.
	ScanClean bool
	// ScanJSON stores the serialized ScanResult for future inspection.
	ScanJSON  string
	StoredAt  time.Time
	ExpiresAt time.Time
	HitCount  int64
	SizeBytes int64
}

CacheEntry stores an artifact path and its scan results.

func (*CacheEntry) IsExpired

func (e *CacheEntry) IsExpired() bool

IsExpired returns true if the entry TTL has elapsed.

type CacheStats

type CacheStats struct {
	Entries   int64
	SizeBytes int64
	HitRatio  float64
	Evictions int64
}

CacheStats holds aggregate statistics about the cache.

type Index

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

Index manages the SQLite-backed metadata index for the local cache.

func NewIndex

func NewIndex(path string) (*Index, error)

NewIndex opens (or creates) a SQLite database at path and applies the schema.

func (*Index) Close

func (idx *Index) Close() error

Close releases the database connection.

func (*Index) Count

func (idx *Index) Count() (int64, error)

Count returns the total number of entries in the index.

func (*Index) Delete

func (idx *Index) Delete(ref *gate.PackageRef) error

Delete removes an entry from the index.

func (*Index) DueForRevalidation

func (idx *Index) DueForRevalidation(before int64, limit int) ([]RevalEntry, error)

DueForRevalidation returns up to limit non-expired entries whose last_validated is older than `before` (a unix timestamp), oldest-first. Expired entries are excluded — they are dropped on access anyway.

func (*Index) Get

func (idx *Index) Get(ref *gate.PackageRef) (*CacheEntry, bool)

Get retrieves a cache entry. Returns (nil, false) if not found or expired.

func (*Index) IncrementHit

func (idx *Index) IncrementHit(ref *gate.PackageRef) error

IncrementHit bumps the hit counter and updates last_hit timestamp.

func (*Index) Insert

func (idx *Index) Insert(ref *gate.PackageRef, entry *CacheEntry) error

Insert adds or updates a cache entry (UPSERT semantics).

func (*Index) LRUCandidates

func (idx *Index) LRUCandidates(n int) ([]gate.PackageRef, error)

LRUCandidates returns up to n entries sorted by last_hit ascending (LRU first).

func (*Index) MarkValidated

func (idx *Index) MarkValidated(ref *gate.PackageRef, ts int64) error

MarkValidated sets last_validated for ref to ts (a unix timestamp).

func (*Index) TotalSizeBytes

func (idx *Index) TotalSizeBytes() (int64, error)

TotalSizeBytes returns the sum of size_bytes for all entries.

type LocalCache

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

LocalCache implements Cache using the local filesystem with a SQLite index.

func NewLocalCache

func NewLocalCache(cfg LocalCacheConfig) (*LocalCache, error)

NewLocalCache creates a LocalCache rooted at cfg.RootPath.

func (*LocalCache) Close

func (lc *LocalCache) Close() error

Close stops the eviction worker and closes the index. Safe to call twice.

func (*LocalCache) DueForRevalidation

func (lc *LocalCache) DueForRevalidation(before int64, limit int) ([]RevalEntry, error)

DueForRevalidation returns cached entries due for re-validation. See Index.

func (*LocalCache) Get

func (lc *LocalCache) Get(ref *gate.PackageRef) (*CacheEntry, bool)

Get returns a cached entry, or (nil, false) on miss or expiry.

func (*LocalCache) Invalidate

func (lc *LocalCache) Invalidate(ref *gate.PackageRef) error

Invalidate removes an entry from both the index and disk.

func (*LocalCache) MarkValidated

func (lc *LocalCache) MarkValidated(ref *gate.PackageRef, ts int64) error

MarkValidated records that ref passed re-validation at ts (unix seconds).

func (*LocalCache) Put

func (lc *LocalCache) Put(ref *gate.PackageRef, tmpPath string, scanClean bool, scanJSON string) error

Put copies the artifact from tmpPath into the cache store and records scan results.

func (*LocalCache) Stats

func (lc *LocalCache) Stats() (CacheStats, error)

Stats returns aggregate cache statistics.

type LocalCacheConfig

type LocalCacheConfig struct {
	RootPath  string
	MaxSizeGB int
	TTL       time.Duration
}

LocalCacheConfig configures the local filesystem cache.

type RevalEntry

type RevalEntry struct {
	Ref       gate.PackageRef
	FilePath  string
	ScanClean bool
	ScanJSON  string
}

RevalEntry is a cached entry presented to the re-validation sweep. It carries just enough to re-run the gates: the package ref, the artifact bytes on disk, and the prior verdict.

Jump to

Keyboard shortcuts

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