disk

package
v3.2.261 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package disk implements a disk-based cache for file data.

Index

Constants

View Source
const (
	// DefaultCacheCapacity is the maximum size of the local cache in bytes if not provided
	// at cache initialization. 0 means unbounded. Equivalent to 2GiB.
	DefaultCapacity = 2 * (1 << 30) // (1 << 30) == 1GiB

	// DefaultMaxAge is the maximum age of cache files, if not provided
	// at cache initialization. 0 means no expiration. Equivalent to 7 days.
	DefaultMaxAge = 7 * 24 * time.Hour

	// DefaultMaxFileCount is the maximum number of files in the cache if not provided
	// at cache initialization. 0 means unbounded.
	DefaultMaxFileCount = 10_000

	// DefaultMaintenanceInterval is the interval at which cache maintenance tasks are performed,
	// if not provided at cache initialization.
	DefaultMaintenanceInterval = 5 * time.Minute

	// DefaultLruFlushInterval is the interval at which the LRU state is persisted to disk,
	// if not provided at cache initialization.
	DefaultLruFlushInterval = 1 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DiskCache

type DiskCache struct {
	// CacheRoot is the root directory for the cache.
	CacheRoot string

	// Capacity is the maximum size of the cache in bytes. 0 means unbounded.
	Capacity int64

	// Disabled indicates whether the cache is disabled.
	//
	// A disabled cache:
	//  - performs no I/O and returns no data.
	//  - allows the caller to operate without caching, while not requiring conditional code around
	//  all cache operations.
	Disabled bool

	// MaxAge is the maximum age of cache files. 0 means no expiration.
	MaxAge time.Duration

	// MaxFileCount is the maximum number of files in the cache. 0 means unbounded.
	MaxFileCount int64

	// MaintenanceInterval is the interval at which cache maintenance tasks are performed.
	//
	// Maintenance tasks include:
	//   - Stats are reloaded from disk
	//   - Old files are deleted based on MaxAge
	MaintenanceInterval time.Duration

	// LruFlushInterval is the interval at which the LRU state is persisted to disk.
	LruFlushInterval time.Duration
	// contains filtered or unexported fields
}

DiskCache implements a simple disk-based cache for file data with an interface that roughly matches the FUSE Read/Write methods.

TODO:

  • allow concurrent reads/writes to different files in the cache by moving lock coordination out of the caller and lock by path within the cache with a RWMutex map. This will require maintaining a reservation of bytes so that concurrent goroutines can't exhaust memory that a different goroutine evicted for its Write operation.
  • consider limiting the max size of individual cache files so that a single large file cannot evict the entire cache
  • allow callers to distiguish between "fatal" vs "non fatal" errors with custom error types e.g. if a read fails, the caller can just read from the source instead of treating it as a fatal error but a write failure because the cache is at capacity and could not make space is more serious
  • figure out platform specific way to disable indexing of cache files e.g. on macOS set the com.apple.metadata:com_apple_backup_excludeItem attribute or use .noindex directory for cache root
  • move Stats#MarshalJSON to a separate file to and apply the filescomfs_debug tag

func NewDiskCache

func NewDiskCache(path string, opts ...Option) (*DiskCache, error)

NewDiskCache creates a DiskCache rooted at path and applies any options. The provided path must be an absolute path to a directory that already exists and is writable by the current process.

If not disabled, it ensures the directory exists and initializes stats by scanning it.

Defaults:

  • Disabled: false
  • Capacity: DefaultCapacity
  • MaxAge: DefaultMaxAge
  • MaxFileCount: DefaultMaxFileCount

func (*DiskCache) Delete

func (dc *DiskCache) Delete(path string) bool

Delete removes the cached file from the cache. It returns true if the file was deleted.

func (*DiskCache) Read

func (dc *DiskCache) Read(path string, buff []byte, ofst int64) (n int, err error)

Read reads data from the cached file at the given path into buff starting at the provided offset.

It returns the number of bytes read, or 0 if the file is not in the cache.

func (*DiskCache) StartMaintenance

func (dc *DiskCache) StartMaintenance()

StartMaintenance starts the maintenance goroutine if it is not already running.

func (*DiskCache) Stats

func (dc *DiskCache) Stats() *cache.Stats

Stats returns the current cache statistics.

func (*DiskCache) StopMaintenance

func (dc *DiskCache) StopMaintenance()

StopMaintenance stops the maintenance goroutine if it is running.

func (*DiskCache) Write

func (dc *DiskCache) Write(path string, buff []byte, ofst int64) (n int, err error)

Write writes data from buff to the cached file at the given path starting at offset ofst. Writing at an offset past the end of the file will grow the file and fill the gap with zeros.

It returns the number of bytes written, or 0 if the cache is not enabled.

type Option

type Option func(*DiskCache)

Option configures a DiskCache.

func WithCapacityBytes

func WithCapacityBytes(n int64) Option

WithCapacityBytes sets the maximum total size (in bytes) for the cache.

func WithDisabled

func WithDisabled(disabled bool) Option

WithDisabled disables the cache (no I/O, no directory creation).

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets the logger for the cache.

func WithLruFlushInterval

func WithLruFlushInterval(d time.Duration) Option

WithLruFlushInterval sets the interval for cache maintenance operations.

func WithMaintenanceInterval

func WithMaintenanceInterval(d time.Duration) Option

WithMaintenanceInterval sets the interval for cache maintenance operations.

func WithMaxAge

func WithMaxAge(d time.Duration) Option

WithMaxAge sets the maximum age for cache files.

func WithMaxFileCount

func WithMaxFileCount(n int64) Option

WithMaxFileCount sets the maximum number of files for the cache.

Jump to

Keyboard shortcuts

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