cache

package
v0.0.0-...-bbc9ce3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2021 License: BSD-2-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package cache provides methods for creating and using a cache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteCache

func DeleteCache(cachepath string) error

DeleteCache removes all traces of a cache located at cachepath.

Types

type Cache

type Cache interface {
	// Adds a value to the cache.
	Add(key Key, value interface{}) interface{}

	// Returns key's value from the cache.
	Get(key Key) (interface{}, bool)

	// Checks if a key exists in cache.
	Contains(key Key) bool

	// Removes a key from the cache.
	Remove(key Key) interface{}

	// Returns the number of items in the cache.
	Len() int

	// Clears all cache entries.
	Clear()
}

Cache is the interface for cache.

type FileCache

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

FileCache represents a cache of files stored somewhere on the disk. It is possible to use the cache concurrently from any number of processes or threads.

func GetFileCache

func GetFileCache(path string, size uint64) (*FileCache, error)

GetFileCache will attempt to load an existing cache if one exists. If one does not it will create a cache with the given size.

func LoadFileCache

func LoadFileCache(path string) (*FileCache, error)

LoadFileCache loads a persistent cache from the file system.

func NewFileCache

func NewFileCache(path string, size uint64) (*FileCache, error)

NewFileCache creates a new persistent cache on the file system.

func (*FileCache) Add

func (fc *FileCache) Add(key FileKey, value io.Reader) (*LinkedFile, error)

Add adds an entry into the cache so that it might later be accessed via Get.

func (*FileCache) Get

func (fc *FileCache) Get(key FileKey) (*LinkedFile, error)

Get returns a LinkedFile to an entry for the given key if one exists.

func (*FileCache) Update

func (fc *FileCache) Update(ctx context.Context)

Update starts a goroutine to periodically check on the status of the cache. The cache can be operated without using Update but if Update is not called the cache's estimate of the cache size will diverge from reality. If many actors are present this can cause the cache size to be very different from this actor's estimate.

type FileKey

type FileKey interface {
	// Hash should return a valid unique file name to be used as the key.
	Hash() string
}

FileKey is the interface of keys that can be used to index cached files.

type Key

type Key interface{}

A Key may be any value that is comparable.

type LRUCache

type LRUCache struct {
	// Size is the maximum number of entries before an item is evicted.
	// Zero means no limit on the number of entries.
	Size uint
	// contains filtered or unexported fields
}

LRUCache is a simple LRU cache.

func (*LRUCache) Add

func (c *LRUCache) Add(key Key, value interface{}) interface{}

Add adds a value to the cache and updates the "recently used"-ness of the key.

func (*LRUCache) Clear

func (c *LRUCache) Clear()

Clear clears all cache entries.

func (*LRUCache) Contains

func (c *LRUCache) Contains(key Key) bool

Contains checks if a key exists in cache without updating the recent-ness.

func (*LRUCache) Get

func (c *LRUCache) Get(key Key) (interface{}, bool)

Get returns key's value from the cache and updates the "recently used"-ness.

func (*LRUCache) Len

func (c *LRUCache) Len() int

Len returns the number of items in the cache.

func (*LRUCache) Remove

func (c *LRUCache) Remove(key Key) interface{}

Remove removes a key from the cache.

type LinkedFile

type LinkedFile string

LinkedFile removes the file it names on closing. This is used to link files into a temporary directory while they're being used so that collection of old files doesn't delete a file that's in use. This effectivelly uses the inode count as a reference counter for the file.

func (LinkedFile) Close

func (l LinkedFile) Close() error

Close deletes the file.

func (LinkedFile) String

func (l LinkedFile) String() string

String returns the name of the file.

Jump to

Keyboard shortcuts

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