fscache

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package fscache implements a file system-based cache backend.

Entries are stored as files in a structured directory hierarchy. Cache entries are stored under the user's OS cache directory by default, with support for custom base directories via the WithBaseDir option or DSN path specification.

The cache supports advanced features including AES-GCM encryption, configurable timeouts, and optional modification time tracking for LRU cleanup strategies. All configuration can be specified through DSN query parameters or programmatic [Option]s passed to Open.

Configuration Parameters

The following DSN query parameters are supported:

  • appname (required): Application subdirectory name for cache isolation
  • timeout (optional): Operation timeout duration (default: 5m)
  • connect_timeout (optional): Cache initialization timeout (default: 5m)
  • encrypt (optional): Enable AES-GCM encryption ("on" or "aesgcm")
  • encrypt_key (optional): Base64-encoded AES key (URL-safe, RFC 4648 §5)
  • update_mtime (optional): Update file mtime on cache hits ("on" to enable)

Usage Examples

The DSN and equavalent programmatic usage are shown below.

 Basic usage:

	fscache://?appname=myapp
	fscache.Open("myapp")

 Custom directory with timeouts:

	fscache:///tmp/cache?appname=myapp&timeout=2s&connect_timeout=10s
	fscache.Open("myapp", fscache.WithBaseDir("/tmp/cache"), fscache.WithTimeout(2*time.Second), fscache.WithConnectTimeout(10*time.Second))

 Encrypted cache:

	fscache://?appname=myapp&encrypt=on&encrypt_key=6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk=
	fscache.Open("myapp", fscache.WithEncryption("6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk="))

 Update modification time on cache hits:

	fscache://?appname=myapp&update_mtime=on
	fscache.Open("myapp", fscache.WithUpdateMTime(true))

Encryption Key Management

Encryption keys can be provided via DSN parameter or environment variable:

export FSCACHE_ENCRYPT_KEY="6S-Ks2YYOW0xMvTzKSv6QD30gZeOi1c6Ydr-As5csWk="

Generate a secure 256-bit encryption key:

openssl rand 32 | base64 | tr '+/' '-_' | tr -d '\n'

LRU Cleanup Integration

When update_mtime is enabled, cache hits update file modification times, enabling efficient LRU cleanup using standard Unix tools:

# Remove files older than 7 days
find /cache/dir -type f -mtime +7 -delete

# Keep only the 1000 most recently used files
find /cache/dir -type f -printf '%T@ %p\n' | sort -rn | tail -n +1001 | cut -d' ' -f2- | xargs rm

Index

Constants

View Source
const Scheme = "fscache" // url scheme for the file system cache

Variables

View Source
var (
	ErrUserCacheDir   = errors.New("fscache: could not determine user cache dir")
	ErrMissingAppName = errors.New("fscache: appname query parameter is required")
	ErrCreateCacheDir = errors.New("fscache: could not create cache dir")
)

Functions

func Open

func Open(appname string, opts ...Option) (*fsCache, error)

Open creates a new file system cache with the specified application name and options.

See the package documentation for supported options and examples.

Types

type Error added in v0.6.0

type Error struct {
	Op  string // operation being performed (e.g., "Get", "Set")
	Key string // optional key for which the operation failed
	Err error  // underlying error
}

func (*Error) Error added in v0.6.0

func (e *Error) Error() string

func (*Error) Unwrap added in v0.6.0

func (e *Error) Unwrap() error

type Option added in v0.6.0

type Option interface {
	// contains filtered or unexported methods
}

func WithBaseDir added in v0.6.0

func WithBaseDir(base string) Option

WithBaseDir sets the base directory for the cache; default: user's OS cache directory.

func WithConnectTimeout added in v0.6.0

func WithConnectTimeout(timeout time.Duration) Option

WithConnectTimeout sets the timeout for establishing the cache connection; default: 5 minutes.

func WithEncryption added in v0.6.0

func WithEncryption(key string) Option

WithEncryption enables encryption for cache entries using AES-GCM.

func WithTimeout added in v0.6.0

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout for cache operations; default: 5 minutes.

func WithUpdateMTime added in v0.12.0

func WithUpdateMTime(enabled bool) Option

WithUpdateMTime enables updating file modification time on cache hits.

Jump to

Keyboard shortcuts

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