cache

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package cache implements durable on-disk cache with LRU expiration.

Index

Constants

View Source
const (
	// DefaultTouchThreshold specifies the resolution of timestamps used to determine which cache items
	// to expire. This helps cache storage writes on frequently accessed items.
	DefaultTouchThreshold = 10 * time.Minute
)
View Source
const DirMode = 0o700

DirMode is the directory mode for all caches.

Variables

This section is empty.

Functions

func BlobIDCacheKey added in v0.10.7

func BlobIDCacheKey(id blob.ID) string

BlobIDCacheKey computes the cache key for the provided blob ID.

func ContentIDCacheKey added in v0.10.7

func ContentIDCacheKey(contentID string) string

ContentIDCacheKey computes the cache key for the provided content ID.

Types

type ContentCache added in v0.10.7

type ContentCache interface {
	Close(ctx context.Context)
	GetContent(ctx context.Context, contentID string, blobID blob.ID, offset, length int64, output *gather.WriteBuffer) error
	PrefetchBlob(ctx context.Context, blobID blob.ID) error
	CacheStorage() Storage
}

ContentCache caches contents stored in pack blobs.

func NewContentCache added in v0.10.7

func NewContentCache(ctx context.Context, st blob.Storage, opt Options, mr *metrics.Registry) (ContentCache, error)

NewContentCache creates new content cache for data contents.

type Options added in v0.10.7

type Options struct {
	BaseCacheDirectory string
	CacheSubDir        string
	Storage            Storage // force particular storage, used for testing
	HMACSecret         []byte
	FetchFullBlobs     bool
	Sweep              SweepSettings
	TimeNow            func() time.Time
}

Options encapsulates all content cache options.

type PersistentCache

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

PersistentCache provides persistent on-disk cache.

func NewPersistentCache

func NewPersistentCache(ctx context.Context, description string, cacheStorage Storage, storageProtection cacheprot.StorageProtection, sweep SweepSettings, mr *metrics.Registry, timeNow func() time.Time) (*PersistentCache, error)

NewPersistentCache creates the persistent cache in the provided storage.

func (*PersistentCache) CacheStorage added in v0.10.6

func (c *PersistentCache) CacheStorage() Storage

CacheStorage returns cache storage.

func (*PersistentCache) Close

func (c *PersistentCache) Close(ctx context.Context)

Close closes the instance of persistent cache possibly waiting for at least one sweep to complete.

func (*PersistentCache) GetFull added in v0.10.6

func (c *PersistentCache) GetFull(ctx context.Context, key string, output *gather.WriteBuffer) bool

GetFull fetches the contents of a full blob. Returns false if not found.

func (*PersistentCache) GetOrLoad

func (c *PersistentCache) GetOrLoad(ctx context.Context, key string, fetch func(output *gather.WriteBuffer) error, output *gather.WriteBuffer) error

GetOrLoad is utility function gets the provided item from the cache or invokes the provided fetch function. The function also appends and verifies HMAC checksums using provided secret on all cached items to ensure data integrity.

func (*PersistentCache) GetPartial added in v0.10.6

func (c *PersistentCache) GetPartial(ctx context.Context, key string, offset, length int64, output *gather.WriteBuffer) bool

GetPartial fetches the contents of a cached blob when (length < 0) or a subset of it (when length >= 0). returns false if not found.

func (*PersistentCache) Put

func (c *PersistentCache) Put(ctx context.Context, key string, data gather.Bytes)

Put adds the provided key-value pair to the cache.

type Storage

type Storage interface {
	blob.Storage
	TouchBlob(ctx context.Context, contentID blob.ID, threshold time.Duration) (time.Time, error)
}

Storage is the storage interface required by the cache and is implemented by the filesystem Storage.

func NewStorageOrNil

func NewStorageOrNil(ctx context.Context, cacheDir string, maxBytes int64, subdir string) (Storage, error)

NewStorageOrNil returns cache.Storage backed by the provided directory.

type SweepSettings added in v0.9.7

type SweepSettings struct {
	// soft limit, the cache will be limited to this size, except for items newer than MinSweepAge.
	MaxSizeBytes int64

	// hard limit, if non-zero the cache will be limited to this size, regardless of MinSweepAge.
	LimitBytes int64

	// items older than this will never be removed from the cache except when the cache is above
	// HardMaxSizeBytes.
	MinSweepAge time.Duration

	// on each use, items will be touched if they have not been touched in this long.
	TouchThreshold time.Duration
}

SweepSettings encapsulates settings that impact cache item sweep/expiration.

Jump to

Keyboard shortcuts

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