disk

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2026 License: Apache-2.0, MIT Imports: 18 Imported by: 0

Documentation

Overview

Package disk provides a disk-backed cache implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockCache

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

BlockCache provides a disk-backed block cache for ByteSources. Blocks are stored as individual files in a directory hierarchy with optional sharding by key prefix. The cache is safe for concurrent use.

func NewBlockCache

func NewBlockCache(dir string, opts ...BlockCacheOption) (*BlockCache, error)

NewBlockCache creates a disk-backed block cache rooted at dir.

func (*BlockCache) MaxBytes

func (c *BlockCache) MaxBytes() int64

MaxBytes returns the configured cache size limit (0 = unlimited).

func (*BlockCache) Prune

func (c *BlockCache) Prune(targetBytes int64) (int64, error)

Prune removes cached entries until the cache is at or below targetBytes.

func (*BlockCache) SizeBytes

func (c *BlockCache) SizeBytes() int64

SizeBytes returns the current cache size in bytes.

func (*BlockCache) Wrap

Wrap returns a ByteSource that caches reads in fixed-size blocks.

type BlockCacheOption

type BlockCacheOption func(*BlockCache)

BlockCacheOption configures a disk-backed block cache.

func WithBlockDirPerm

func WithBlockDirPerm(mode os.FileMode) BlockCacheOption

WithBlockDirPerm sets the directory permissions used for cache directories.

func WithBlockLogger

func WithBlockLogger(logger *slog.Logger) BlockCacheOption

WithBlockLogger sets the logger for block cache operations. If not set, logging is disabled.

func WithBlockMaxBytes

func WithBlockMaxBytes(n int64) BlockCacheOption

WithBlockMaxBytes sets the maximum size in bytes for the block cache. Values <= 0 disable the limit.

func WithBlockShardPrefixLen

func WithBlockShardPrefixLen(n int) BlockCacheOption

WithBlockShardPrefixLen sets the number of hex characters used for sharding. Use 0 to disable sharding. Defaults to 2.

type Cache

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

Cache implements cache.Cache using the local filesystem. Files are stored in a directory hierarchy with optional sharding by hash prefix. The cache is safe for concurrent use.

func New

func New(dir string, opts ...Option) (*Cache, error)

New creates a disk-backed cache rooted at dir.

func (*Cache) Delete

func (c *Cache) Delete(hash []byte) error

Delete removes cached content for the given hash.

func (*Cache) Get

func (c *Cache) Get(hash []byte) (fs.File, bool)

Get returns an fs.File for reading cached content. Returns nil, false if the content is not cached.

func (*Cache) MaxBytes

func (c *Cache) MaxBytes() int64

MaxBytes returns the configured cache size limit (0 = unlimited).

func (*Cache) Prune

func (c *Cache) Prune(targetBytes int64) (int64, error)

Prune removes cached entries until the cache is at or below targetBytes.

func (*Cache) Put

func (c *Cache) Put(hash []byte, f fs.File) error

Put stores content by reading from the provided fs.File. The cache reads the file to completion; caller still owns/closes the file.

func (*Cache) SizeBytes

func (c *Cache) SizeBytes() int64

SizeBytes returns the current cache size in bytes.

type Option

type Option func(*Cache)

Option configures a disk cache.

func WithDirPerm

func WithDirPerm(mode os.FileMode) Option

WithDirPerm sets the directory permissions used for cache directories.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for cache operations. If not set, logging is disabled.

func WithMaxBytes

func WithMaxBytes(n int64) Option

WithMaxBytes sets the maximum cache size in bytes. Values < 0 are invalid. Use 0 to disable the limit.

func WithShardPrefixLen

func WithShardPrefixLen(n int) Option

WithShardPrefixLen sets the number of hex characters used for sharding. Use 0 to disable sharding. Defaults to 2.

Jump to

Keyboard shortcuts

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