diskcache

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEntryTooLarge = errors.New("entry size exceeds maximum allowed size")
	ErrCacheFull     = errors.New("cache is full, unable to add new entry")
)

Functions

This section is empty.

Types

type Cache

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

func NewCache

func NewCache(baseDir string, maxSize int64, opts ...CacheOption) (*Cache, error)

func (*Cache) Adopt

func (c *Cache) Adopt(key Key)

Adopt attempts to take ownership of an existing file on disk and add it to the cache under the given key, without copying the file content. This is useful for cases where the file content is already on disk and can be added to the cache without needing to be written again, such as when the file is generated by an external process or when migrating existing files into the cache. note: sizing rules on entry's and cache do not apply with this function. so no eviction will occur and the entry will be added even if it exceeds maxEntrySize or maxSize.

func (*Cache) BaseDir

func (c *Cache) BaseDir() string

func (*Cache) Delete

func (c *Cache) Delete(key Key) bool

Delete removes the entry associated with the given key from the cache and deletes the corresponding file from disk. Returns true if the entry was found and deleted, false if the key was not found in the cache. If the file cannot be deleted, the entry will be marked as invalid and will attempt deletion on the next access.

func (*Cache) Get

func (c *Cache) Get(key Key) (found bool, bytes []byte)

func (*Cache) Pin

func (c *Cache) Pin(key Key) (path string, release func(), ok bool)

Pin marks a cache entry as in-use and returns its on-disk path. The entry cannot be evicted until Release is called. Caution: the caller must call the release function when it is done with the entry to avoid memory leaks. Caution: the file content may be modified on disk after Pin returns, so the caller should not assume the content is immutable or consistent with the content at the time of the Pin call. Use Get if you need a consistent snapshot of the content. Pin is intended for use cases where the caller needs to access the file on disk and is able to handle potential modifications to the file content, such as by re-reading the file after a modification is detected. Returns ok=false if the key is not in the cache.

func (*Cache) Set

func (c *Cache) Set(key Key, bytes []byte) error

Set adds a new entry to cache or updates an existing entry size of bytes <= maxEntrySize. if current size > max size before add or updating , it will attempt to reconcile by evicting old entries until the cache is under budget if it cannot be reconciled, the new entry will not be added and an error will be returned.

func (*Cache) SetPin

func (c *Cache) SetPin(key Key, bytes []byte) (string, func(), error)

func (*Cache) WarmUp

func (c *Cache) WarmUp(walkFunc WalkFunc) error

WarmUp walks the cache's base directory and attempts to adopt files into the cache using the provided walkFunc to determine the corresponding cache key for each file. This is useful for pre-populating the cache with existing files on disk, such as after a restart or when initializing the cache with pre-generated content. The walkFunc should return the cache key corresponding to the given file path, and a boolean indicating whether the file should be adopted into the cache (true) or skipped (false).

type CacheOption

type CacheOption func(*Cache)

func WithFileMode

func WithFileMode(mode os.FileMode) CacheOption

func WithHashFunc

func WithHashFunc(hashFunc func([]byte) (uint64, error)) CacheOption

func WithMaxEntrySize

func WithMaxEntrySize(maxEntrySize int64) CacheOption

func WithOnEviction

func WithOnEviction(callback func(key Key)) CacheOption

type Key

type Key struct {
	Key  string
	Path string
}

type WalkFunc

type WalkFunc func(path string) (key Key, ok bool)

type WarmUpError

type WarmUpError struct {
	Errs []error
}

func (*WarmUpError) Error

func (e *WarmUpError) Error() string

Jump to

Keyboard shortcuts

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