cache

package
v1.222.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache provides generic file-based caching with platform-specific 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 platform-specific 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.
	// On Unix, this uses flock with bounded retry logic.
	// On Windows, this executes without locking (graceful degradation).
	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.
	// On Windows, this executes without locking (graceful degradation).
	WithLockContext(ctx context.Context, fn func() error) error

	// WithRLock executes fn while holding a shared read lock.
	// On Unix, this uses flock with read lock.
	// On Windows, this executes without locking (graceful degradation).
	WithRLock(fn func() error) error
}

FileLock provides file-level locking for cache operations. Implementations are platform-specific to handle differences between Unix (using flock) and Windows (graceful degradation).

func NewFileLock

func NewFileLock(path string) FileLock

NewFileLock creates a new FileLock for the given path. The lock file is created at path + ".lock" to prevent lock loss during atomic renames.

Jump to

Keyboard shortcuts

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