Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v1.224.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package cache provides generic file-based caching with platform-appropriate locking.

Index

Constants

View Source
const (
	// DefaultCacheDirPerm is the default permission for cache directories.
	DefaultCacheDirPerm = 0o755
	// DefaultFilePerm is the default permission for cache files.
	DefaultFilePerm = 0o644
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FileCache

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

FileCache provides atomic file-based caching with cross-process locking. It stores cached content in an XDG-compliant cache directory.

func NewFileCache

func NewFileCache(subpath string, opts ...FileCacheOption) (*FileCache, error)

NewFileCache creates a new FileCache in the specified XDG cache subdirectory. The subpath is relative to the XDG cache directory (e.g., "stack-imports" creates ~/.cache/atmos/stack-imports/).

func (*FileCache) BaseDir

func (c *FileCache) BaseDir() string

BaseDir returns the base directory of the cache.

func (*FileCache) Clear

func (c *FileCache) Clear() error

Clear removes all cached entries from the cache directory.

func (*FileCache) Get

func (c *FileCache) Get(key string) ([]byte, bool, error)

Get returns the cached content for a key. Returns (content, true, nil) if found, (nil, false, nil) if not found, or (nil, false, error) on read error.

func (*FileCache) GetOrFetch

func (c *FileCache) GetOrFetch(key string, fetch func() ([]byte, error)) ([]byte, error)

GetOrFetch returns cached content for a key, or calls fetch() and caches the result. This provides a convenient way to implement cache-aside patterns.

func (*FileCache) GetPath

func (c *FileCache) GetPath(key string) (string, bool)

GetPath returns the filesystem path for a cached key. Returns (path, true) if the key exists in cache, (path, false) otherwise. This is useful when callers need the file path rather than content.

func (*FileCache) Set

func (c *FileCache) Set(key string, content []byte) error

Set stores content for a key atomically.

type FileCacheOption

type FileCacheOption func(*FileCache)

FileCacheOption is a functional option for configuring FileCache.

func WithBaseDir

func WithBaseDir(dir string) FileCacheOption

WithBaseDir sets a custom base directory for the cache. This is primarily useful for testing.

func WithFileSystem

func WithFileSystem(fs filesystem.FileSystem) FileCacheOption

WithFileSystem sets a custom filesystem implementation. This is primarily useful for testing.

type FileLock

type FileLock interface {
	// WithLock executes fn while holding an exclusive lock where supported.
	// Windows cache operations execute best-effort without native file locking.
	WithLock(fn func() error) error

	// WithLockContext executes fn while holding an exclusive lock, blocking until
	// the lock is acquired or ctx is done. Unlike WithLock's bounded retry, it
	// waits the full duration of whatever the holder is doing (e.g. a multi-second
	// download), making it suitable for collapsing a herd around a slow operation.
	// Windows cache operations execute best-effort without native file locking.
	WithLockContext(ctx context.Context, fn func() error) error

	// WithRLock executes fn while holding a shared read lock where supported.
	// Windows cache reads execute best-effort without native file locking.
	WithRLock(fn func() error) error
}

FileLock provides file-level locking for cache operations.

func NewFileLock

func NewFileLock(path string) FileLock

NewFileLock preserves the cache package API while using a stable sibling lock file that survives atomic cache-file replacement.

Jump to

Keyboard shortcuts

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