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

cache

package
v0.1.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheFull = errors.New("cache: storage capacity exceeded")

ErrCacheFull is returned when the cache has reached its maximum capacity.

View Source
var ErrInvalidPath = errors.New("cache: invalid path (traversal attempt)")

ErrInvalidPath is returned when a bucket or key would escape the cache root.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Put writes data to the cache under the given bucket/key and returns
	// the resulting metadata (path, size, etag, checksum).
	// The data is fsync'd before returning to guarantee durability.
	// Returns ErrCacheFull if the cache has reached its maximum capacity.
	// Returns ErrInvalidPath if bucket/key would escape the cache root.
	Put(ctx context.Context, bucket, key string, r io.Reader) (*ObjectInfo, error)

	// PutStaged writes data to a temp file (fsync'd) without replacing the
	// existing cache entry. Returns a StagedObject whose Commit method
	// atomically renames the temp file to the final path. If Commit is not
	// called, Rollback removes the temp file.
	// Callers choose whether to commit before or after their DB transaction.
	PutStaged(ctx context.Context, bucket, key string, r io.Reader) (*StagedObject, error)

	// Get opens a cached object for reading. Returns os.ErrNotExist if the
	// object is not in the cache.
	// Returns ErrInvalidPath if bucket/key would escape the cache root.
	Get(ctx context.Context, bucket, key string) (io.ReadCloser, *ObjectInfo, error)

	// Delete removes an object from the cache.
	// Returns ErrInvalidPath if bucket/key would escape the cache root.
	Delete(ctx context.Context, bucket, key string) error

	// Exists reports whether an object is present in the cache.
	Exists(ctx context.Context, bucket, key string) bool

	// UsedBytes returns the total bytes consumed by cached objects.
	UsedBytes() int64

	// CreateBucketDir ensures the directory for a bucket exists.
	// Returns ErrInvalidPath if bucket would escape the cache root.
	CreateBucketDir(ctx context.Context, bucket string) error

	// DeleteBucketDir removes the cache directory for a bucket.
	// Returns ErrInvalidPath if bucket would escape the cache root.
	DeleteBucketDir(ctx context.Context, bucket string) error

	// PutPart writes a multipart upload part to the cache.
	// Parts are stored under .multipart/<uploadID>/<partNumber>.
	// Returns ObjectInfo with the part's Size, ETag (MD5), and Checksum (SHA-256).
	PutPart(ctx context.Context, uploadID string, partNumber int, r io.Reader) (*ObjectInfo, error)

	// AssembleParts concatenates the specified parts in order into a final
	// object at bucket/key. Returns ObjectInfo for the assembled object and
	// the ordered list of individual part MD5 hex digests (for S3 ETag computation).
	// The part files are NOT deleted; call DeleteUpload to clean up.
	AssembleParts(ctx context.Context, bucket, key, uploadID string, partNumbers []int) (*ObjectInfo, []string, error)

	// DeleteUpload removes all part files for the given upload ID.
	DeleteUpload(ctx context.Context, uploadID string) error
}

Cache defines the interface for local object caching. Implementations must be safe for concurrent use.

type EvictionPolicy

type EvictionPolicy string

EvictionPolicy controls when remotely durable objects are removed from the local cache.

const (
	EvictionPolicyLRU         EvictionPolicy = "lru"
	EvictionPolicyAfterUpload EvictionPolicy = "after_upload"
	EvictionPolicyNone        EvictionPolicy = "none"
)

func ParseEvictionPolicy

func ParseEvictionPolicy(value string) (EvictionPolicy, bool)

ParseEvictionPolicy returns the canonical eviction policy.

func (EvictionPolicy) EnqueuesAfterUploadEviction

func (p EvictionPolicy) EnqueuesAfterUploadEviction() bool

EnqueuesAfterUploadEviction reports whether upload finalization must create an eviction task in the same transaction.

type Filesystem

type Filesystem struct {
	// contains filtered or unexported fields
}

Filesystem implements the Cache interface using the local filesystem.

func NewFilesystem

func NewFilesystem(dir string, maxBytes int64) (*Filesystem, error)

NewFilesystem creates a filesystem-backed cache rooted at dir. maxBytes sets the capacity limit (0 = unlimited). On startup it walks existing files to initialise usedBytes and removes stale temp files.

func (*Filesystem) AssembleParts

func (f *Filesystem) AssembleParts(_ context.Context, bucket, key, uploadID string, partNumbers []int) (*ObjectInfo, []string, error)

func (*Filesystem) CreateBucketDir

func (f *Filesystem) CreateBucketDir(_ context.Context, bucket string) error

func (*Filesystem) Delete

func (f *Filesystem) Delete(_ context.Context, bucket, key string) error

func (*Filesystem) DeleteBucketDir

func (f *Filesystem) DeleteBucketDir(_ context.Context, bucket string) error

func (*Filesystem) DeleteUpload

func (f *Filesystem) DeleteUpload(_ context.Context, uploadID string) error

func (*Filesystem) Exists

func (f *Filesystem) Exists(_ context.Context, bucket, key string) bool

func (*Filesystem) Get

func (f *Filesystem) Get(_ context.Context, bucket, key string) (io.ReadCloser, *ObjectInfo, error)

func (*Filesystem) Put

func (f *Filesystem) Put(ctx context.Context, bucket, key string, r io.Reader) (*ObjectInfo, error)

func (*Filesystem) PutPart

func (f *Filesystem) PutPart(_ context.Context, uploadID string, partNumber int, r io.Reader) (*ObjectInfo, error)

func (*Filesystem) PutStaged

func (f *Filesystem) PutStaged(ctx context.Context, bucket, key string, r io.Reader) (*StagedObject, error)

func (*Filesystem) RootDir

func (f *Filesystem) RootDir() string

RootDir returns the absolute path of the cache root directory.

func (*Filesystem) UsedBytes

func (f *Filesystem) UsedBytes() int64

type ObjectInfo

type ObjectInfo struct {
	Path     string
	Size     int64
	ETag     string
	Checksum string // SHA-256 hex digest
}

ObjectInfo holds metadata about a cached object.

type StagedObject

type StagedObject struct {
	Info *ObjectInfo
	// contains filtered or unexported fields
}

StagedObject represents a cached file that has been written to a temp location but not yet committed to the final cache path. This enables atomic "write then commit" workflows where a DB transaction runs between write and commit.

func (*StagedObject) Commit

func (s *StagedObject) Commit() error

Commit renames the staged file to the final cache path (atomic). Must be called exactly once. After Commit, Rollback is a no-op.

func (*StagedObject) Rollback

func (s *StagedObject) Rollback() error

Rollback removes the staged temp file without affecting the final cache path. Safe to call multiple times and after Commit (no-op if already committed).

Jump to

Keyboard shortcuts

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