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

cache

package
v1.221.0 Latest Latest
Warning

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

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

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 retry logic.
	// On Windows, this executes without locking (graceful degradation).
	WithLock(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