diskcache

package
v0.0.0-...-75b7e41 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvictStats

type EvictStats struct {
	// CacheSize is the size of the cache before evicting.
	CacheSize int64

	// Evicted is the number of items evicted.
	Evicted int
}

EvictStats is information gathered during Evict.

type Fetcher

type Fetcher func(context.Context) (io.ReadCloser, error)

Fetcher returns a ReadCloser. It is used by Open if the key is not in the cache.

type File

type File struct {
	*os.File

	// The Path on disk for File
	Path string
}

File is an os.File, but includes the Path

type Store

type Store struct {
	// Dir is the directory to cache items.
	Dir string

	// Component when set is reported to OpenTracing as the component.
	Component string

	// BackgroundTimeout when non-zero will do fetches in the background with
	// a timeout. This means the context passed to fetch will be
	// context.WithTimeout(context.Background(), BackgroundTimeout). When not
	// set fetches are done with the passed in context.
	BackgroundTimeout time.Duration

	// BeforeEvict, when non-nil, is a function to call before evicting a file.
	// It is passed the path to the file to be evicted.
	BeforeEvict func(string)

	// MaxCacheSizeBytes is the maximum size of the cache directory after evicting
	// entries.
	MaxCacheSizeBytes int64
}

Store is an on disk cache, with items cached via calls to Open.

func (*Store) Evict

func (s *Store) Evict()

Evict will remove files from Store.Dir until it is smaller than Store.MaxCacheSizeBytes. It evicts files with the oldest modification time first.

func (*Store) EvictMaxSize

func (s *Store) EvictMaxSize(maxCacheSizeBytes int64) (stats EvictStats, err error)

Evict will remove files from Store.Dir until it is smaller than maxCacheSizeBytes. It evicts files with the oldest modification time first.

func (*Store) Open

func (s *Store) Open(ctx context.Context, key string, fetcher Fetcher) (file *File, err error)

Open will open a file from the local cache with key. If missing, fetcher will fill the cache first. Open also performs single-flighting for fetcher.

Jump to

Keyboard shortcuts

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