cache

package
v0.0.0-...-1f88c41 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: Apache-2.0 Imports: 21 Imported by: 4

Documentation

Overview

Package cache implements both in-memory and on-disk caching.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHash = errors.New("invalid hash")

ErrInvalidHash indicates invalid hash is specified.

Functions

This section is empty.

Types

type Cache

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

Cache is a cache of objects holding content in disk.

All implementations must be thread-safe.

func New

func New(policies Policies, path string, h crypto.Hash) (*Cache, error)

New creates a disk based cache.

It may return both a valid Cache and an error if it failed to load the previous cache metadata. It is safe to ignore this error. This creates cache directory if it doesn't exist.

func (*Cache) Add

func (d *Cache) Add(ctx context.Context, digest HexDigest, src io.Reader) error

Add reads data from src and stores it in cache.

func (*Cache) AddFileWithoutValidation

func (d *Cache) AddFileWithoutValidation(ctx context.Context, digest HexDigest, src string) error

AddFileWithoutValidation adds src as cache entry with hardlink. But this doesn't do any content validation.

TODO(tikuta): make one function and control the behavior by option?

func (d *Cache) AddWithHardlink(ctx context.Context, digest HexDigest, src io.Reader, dest string, perm os.FileMode) error

AddWithHardlink reads data from src and stores it in cache and hardlink file. This is to avoid file removal by shrink in Add().

func (*Cache) Added

func (d *Cache) Added() []int64

Added returns a list of file size added to cache.

func (*Cache) Close

func (d *Cache) Close() error

Close closes the Cache, writes the cache status file to cache dir.

func (*Cache) Evict

func (d *Cache) Evict(digest HexDigest)

Evict removes item from cache if it's there.

func (d *Cache) Hardlink(digest HexDigest, dest string, perm os.FileMode) error

Hardlink ensures file at |dest| has the same content as cached |digest|.

Note that the behavior when dest already exists is undefined. It will work on all POSIX and may or may not fail on Windows depending on the implementation used. Do not rely on this behavior.

func (*Cache) Keys

func (d *Cache) Keys() HexDigests

Keys returns the list of all cached digests in LRU order.

func (*Cache) Read

func (d *Cache) Read(digest HexDigest) (io.ReadCloser, error)

Read returns contents of the cached item.

func (*Cache) TotalSize

func (d *Cache) TotalSize() units.Size

TotalSize returns the size of the contents maintained in the LRU cache.

func (*Cache) Touch

func (d *Cache) Touch(digest HexDigest) bool

Touch updates the LRU position of an item to ensure it is kept in the cache.

Returns true if item is in cache.

func (*Cache) Used

func (d *Cache) Used() []int64

Used returns a list of file size used from cache.

type HexDigest

type HexDigest string

HexDigest is the hash of a file that is hex-encoded. Only lower case letters are accepted.

func Hash

func Hash(h crypto.Hash, src io.Reader) (HexDigest, error)

Hash hashes a reader and returns a HexDigest from it.

func HashBytes

func HashBytes(h crypto.Hash, content []byte) HexDigest

HashBytes hashes content and returns a HexDigest from it.

func Sum

func Sum(h hash.Hash) HexDigest

Sum is a shortcut to get a HexDigest from a hash.Hash.

func (HexDigest) Validate

func (d HexDigest) Validate(h crypto.Hash) bool

Validate returns true if the hash is valid.

type HexDigests

type HexDigests []HexDigest

HexDigests is a slice of HexDigest that implements sort.Interface.

func (HexDigests) Len

func (h HexDigests) Len() int

func (HexDigests) Less

func (h HexDigests) Less(i, j int) bool

func (HexDigests) Swap

func (h HexDigests) Swap(i, j int)

type Policies

type Policies struct {
	// MaxSize trims if the cache gets larger than this value. If 0, the cache is
	// effectively a leak.
	MaxSize units.Size
	// MaxItems is the maximum number of items to keep in the cache. If 0, do not
	// enforce a limit.
	MaxItems int
	// MinFreeSpace trims if disk free space becomes lower than this value.
	// Only makes sense when using disk based cache.
	MinFreeSpace units.Size
}

Policies is the policies to use on a cache to limit it's footprint.

It's a cache, not a leak.

func (*Policies) AddFlags

func (p *Policies) AddFlags(f *flag.FlagSet)

AddFlags adds flags for cache policy parameters.

func (*Policies) IsDefault

func (p *Policies) IsDefault() bool

IsDefault returns whether some flags are set or not.

Jump to

Keyboard shortcuts

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