filecache

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: AGPL-3.0 Imports: 11 Imported by: 0

README

A simple LRU FileCache with file freshness monitor.

FileCache accepts a max size and age, and will remove items if they become too old or at end of the LRU list.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = errors.Define("filecache: file not found")
	ErrAlreadyExists = errors.Define("filecache: file at path already exists")
	ErrFailedRefresh = errors.Define("filecache: failed refreshing cache entry")
	ErrFileTooLarge  = errors.Define("filecache: file too large")
	ErrClosed        = errors.Define("filecache: closed")
)
View Source
var DefaultConfig = Config{
	RefreshFreq:      time.Minute,
	Size:             100,
	MaxSize:          int64(1024 * 1000 * 5),
	MaxAge:           time.Minute * 30,
	EvictionCallback: func(path string, file File) { log.Printf("FileCache evicted '%s'", path) },
	ErrorEvictionCallback: func(path string, file File, err error) {
		log.Printf("FileCache evicted '%s' due to error: %v", path, err)
	},
}

Functions

This section is empty.

Types

type CacheEntry

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

func (*CacheEntry) Bytes

func (e *CacheEntry) Bytes() []byte

func (*CacheEntry) Reader

func (e *CacheEntry) Reader() io.Reader

func (*CacheEntry) Release

func (e *CacheEntry) Release()

type Config

type Config struct {
	// RefreshFreq is the frequency at which the refresh routines operates
	RefreshFreq time.Duration

	// Size is the fixed size of the FileCache
	Size int

	// MaxSize is the maximum size of files permitted in the cache
	MaxSize int64

	// MaxAge is the maximum time entries can go unused in
	// the cache before being marked for eviction. The minimum age
	// (and granularity) is 1 second
	MaxAge time.Duration

	// AllowOverwrites signifies whether to overwrite existing
	// files of the same path when passed to .Put()
	AllowOverwrites bool

	// EvictionCallback is called when a file is evicted from the FileCache
	EvictionCallback func(path string, file File)

	// ErrorEvictionCallback is called when a file is evicted from the FileCache
	// due to an error, e.g. cannot stat, file too large, etc
	ErrorEvictionCallback func(path string, file File, err error)
}

type File

type File interface {
	Open() (io.ReadCloser, error)
	Stat() (os.FileInfo, error)
}

type FileCache

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

FileCache is an LRU cache with filesystem monitoring. UpgradeableMutex is built-in to the FileCache and CachedFile objects it manages for multi-threading

func New

func New(cfg *Config) *FileCache

NewFileCache returns a new FileCache object of size, and max file age

func (*FileCache) Get

func (fc *FileCache) Get(path string) (*CacheEntry, error)

func (*FileCache) Put

func (fc *FileCache) Put(path string, file File) error

Put places a CachedFile object in the FileCache.

func (*FileCache) Remove

func (fc *FileCache) Remove(path string)

Jump to

Keyboard shortcuts

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