Documentation
¶
Overview ¶
Package cache provides generic file-based caching with platform-specific locking.
Index ¶
- Constants
- type FileCache
- func (c *FileCache) BaseDir() string
- func (c *FileCache) Clear() error
- func (c *FileCache) Get(key string) ([]byte, bool, error)
- func (c *FileCache) GetOrFetch(key string, fetch func() ([]byte, error)) ([]byte, error)
- func (c *FileCache) GetPath(key string) (string, bool)
- func (c *FileCache) Set(key string, content []byte) error
- type FileCacheOption
- type FileLock
Constants ¶
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) Get ¶
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 ¶
GetOrFetch returns cached content for a key, or calls fetch() and caches the result. This provides a convenient way to implement cache-aside patterns.
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 ¶
NewFileLock creates a new FileLock for the given path. The lock file is created at path + ".lock" to prevent lock loss during atomic renames.