cache

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: BSD-3-Clause Imports: 9 Imported by: 1

Documentation

Overview

Package cache implements the data store behind Airlift server.

Index

Constants

View Source
const SHASize = 64

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// OnRemove is an arbitrary callback that is called whenever a file is
	// successfully removed.
	OnRemove func(id string)

	*sync.RWMutex
	// contains filtered or unexported fields
}

Cache is an extremely naïve, map-based, fully in-memory key-value store configured as a file cache. Only file locations are stored in memory. Persistence is achieved through the file system. It is concurrent-access safe through locking.

func New

func New(dirPath string) (*Cache, error)

New initializes and returns a new cache object rooted in dirPath.

func (*Cache) CutToCount

func (c *Cache) CutToCount(n int) (m int, err error)

CutCoCount removes the oldest file until the number of files in the cache is less than or equal to n.

func (*Cache) CutToSize

func (c *Cache) CutToSize(n int64) ([]string, error)

CutToSize removes the oldest file in the cache until the total size is at most n bytes, returning the IDs of the deleted files. Nothing will happen if n == 0. To remove all files, use RemoveAll.

func (*Cache) Get

func (c *Cache) Get(id string) string

GetFile returns the path to the file with the given ID in the cache.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of files in the cache.

func (*Cache) MaybeCutToSize

func (c *Cache) MaybeCutToSize(n int64) (m int)

MaybeCutToSize returns how many files would be pruned if CutToSize would have been called.

func (*Cache) MaybeRemoveOlderThan

func (c *Cache) MaybeRemoveOlderThan(t time.Time) int

MaybeRemoveOlderThan returns the number of files that would be deleted if RemoveOlderThan were to be called.

func (*Cache) Put

func (c *Cache) Put(content io.Reader, filename string, conf Config) (string, error)

Put copies a file to disk with the given filename and returns its hash.

func (*Cache) Remove

func (c *Cache) Remove(id string) error

Remove removes a file from the cache.

func (*Cache) RemoveAll

func (c *Cache) RemoveAll() error

RemoveAll removes every file in the cache.

func (*Cache) RemoveNewest

func (c *Cache) RemoveNewest() (string, error)

RemoveNewest removes the most recently modified item in the cache. It returns the ID of the file that was removed and an error if one was encountered.

func (*Cache) RemoveOlderThan

func (c *Cache) RemoveOlderThan(t time.Time) ([]string, error)

RemoveOlderThan removes all files in the cache that were modified before t, returning the IDs of the deleted files. If an error is encountered while deleting a file, it will not advance any further.

func (*Cache) SetDir

func (c *Cache) SetDir(dir string)

SetDir sets the base directory where files will be stored on disk.

func (*Cache) Size

func (c *Cache) Size() int64

Size returns the total number of bytes taken up by files in the cache.

func (*Cache) SortedIDs

func (c *Cache) SortedIDs() []string

SortedIDs returns a slice of every cached file's ID, sorted descending by modification time.

func (*Cache) Stat

func (c *Cache) Stat(id string) os.FileInfo

Stat returns the os.FileInfo associated with a file.

func (*Cache) WatchAges

func (c *Cache) WatchAges(conf Config)

WatchAges starts a blocking server that constantly watches for files to become stale, and then deletes them. It should be run in its own goroutine.

type Config

type Config interface {
	MaxAge() int
	MaxSize() int64
	MaxCount() int
	Refresh()
	ProcessHash(buf []byte) string
}

Config represents the configurable behavior that Cache needs to operate.

Jump to

Keyboard shortcuts

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