cache

package
v1.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache provides disk-based caching for Git packfiles with in-memory metadata.

The cache manager stores packfiles on disk and uses Ristretto for in-memory metadata caching. It supports size-based eviction, concurrent access, and provides statistics about cache usage.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToCreateCacheDirectory = errors.New("failed to create cache directory")
	ErrFailedToCreateRistrettoCache = errors.New("failed to create ristretto cache")
	ErrFailedToCalculateDiskUsage   = errors.New("failed to calculate disk usage")
	// ErrCacheEntryNotFound means the entry is not currently in the cache. It
	// covers both an entry absent from the in-memory index (never stored, or
	// evicted) and one whose backing file has gone from disk. Callers use
	// IsNotFound to tell an evicted entry from a genuine I/O error.
	ErrCacheEntryNotFound      = errors.New("cache entry not found")
	ErrCachedFileNotFound      = errors.New("cached file not found")
	ErrFailedToCreateCacheFile = errors.New("failed to create cache file")
	ErrFailedToWriteCacheFile  = errors.New("failed to write cache file")
	ErrFailedToRenameCacheFile = errors.New("failed to rename cache file")
	ErrFailedToRemoveCacheFile = errors.New("failed to remove cache file")
)

Functions

func IsNotFound added in v1.28.2

func IsNotFound(err error) bool

IsNotFound reports whether err indicates the cache entry is not currently retrievable (never stored, evicted, or its file is gone from disk), as opposed to a genuine I/O error.

Types

type Config

type Config struct {
	// CacheDir is the directory to store packfiles
	CacheDir string
	// MaxSizeBytes is the maximum cache size in bytes
	MaxSizeBytes int64
	// MaxEntries is the maximum number of entries to keep in memory
	MaxEntries int64
	// MaxIdleAge, if non-zero, enables a sliding TTL on cache entries: an
	// entry expires when it has been idle (no Get) for at least this long.
	// Every Get renews the TTL. Zero disables the TTL and relies solely on
	// size-based (LFU-ish) eviction against MaxSizeBytes.
	MaxIdleAge time.Duration
}

Config holds configuration for the cache manager.

type Entry

type Entry struct {
	// Key is the cache key
	Key string
	// FilePath is the path to the cached packfile on disk
	FilePath string
	// Size is the size of the packfile in bytes
	Size int64
	// CreatedAt is when the entry was created
	CreatedAt time.Time
	// LastAccessed is when the entry was last accessed
	LastAccessed time.Time
}

Entry represents metadata about a cached packfile.

type IdleBucket added in v1.29.0

type IdleBucket struct {
	Threshold time.Duration
	Entries   int64
	Bytes     int64
}

IdleBucket is the number of live cache entries, and their total bytes, whose time since last access is at least Threshold.

type Manager

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

Manager handles caching of Git packfiles.

func New

func New(cfg Config) (*Manager, error)

New creates a new cache manager.

func (*Manager) Clear

func (m *Manager) Clear(ctx context.Context) error

Clear removes all cached entries.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, key string) error

Delete removes a cached entry.

func (*Manager) DiskStats added in v1.25.0

func (m *Manager) DiskStats() (bytes, entries int64, err error)

DiskStats returns the current on-disk cache size in bytes and the number of cached packfiles, by walking the cache directory.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, key string) (io.ReadCloser, int64, error)

Get retrieves a cached packfile by key Returns a ReadCloser for the packfile, or nil if not found.

func (*Manager) Has

func (m *Manager) Has(ctx context.Context, key string) bool

Has checks if a key exists in the cache.

func (*Manager) IdleStats added in v1.29.0

func (m *Manager) IdleStats(thresholds []time.Duration) []IdleBucket

IdleStats reports, for each threshold, how many live cache entries (and how many bytes) have not been read for at least that long. Buckets are cumulative: an entry idle for 2h counts toward the 1h, 15m, and 5m thresholds. The result is aligned with the thresholds slice.

func (*Manager) Limits added in v1.25.0

func (m *Manager) Limits() (maxSizeBytes, maxEntries int64)

Limits returns the configured cache capacity limits: the maximum size in bytes and the maximum number of entries.

func (*Manager) Stats

func (m *Manager) Stats() Stats

Stats returns cache statistics.

func (*Manager) Store

func (m *Manager) Store(ctx context.Context, key string, reader io.Reader) (string, int64, error)

Store saves a packfile to disk and caches its metadata The reader will be fully consumed and the packfile stored to disk.

type Stats

type Stats struct {
	CurrentSize  int64
	MaxSize      int64
	UsagePercent float64
}

Stats represents cache statistics.

Jump to

Keyboard shortcuts

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