cache

package
v4.24.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2017 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package cache lazily loads information on demand and caches them. The data inside a cache has to implement the Cacheable interface which also contains methods for checking, if the information is outdated, and for discarding the cached instance. It is loaded by a user defined CacheableLoader function.

Index

Constants

View Source
const (
	ErrSettingOptions = iota + 1
	ErrIllegalCache
	ErrNoLoader
	ErrLoading
	ErrCheckOutdated
	ErrDiscard
	ErrDiscardedWhileLoading
	ErrTimeout
	ErrFileLoading
	ErrFileSize
	ErrFileChecking
)

Errors of the Cache.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Load returns a Cacheable from memory or source.
	Load(id string, timeout time.Duration) (Cacheable, error)

	// Discard explicitly removes a Cacheable from Cache. Normally
	// done automatically.
	Discard(id string) error

	// Clear empties the Cache.
	Clear() error

	// Len returns the number of entries in the Cache.
	Len() int

	// Stop tells the Cache to stop working.
	Stop() error
}

Cache loads and returns instances by ID and caches them in memory.

func New

func New(options ...Option) (Cache, error)

New creates a new cache.

type Cacheable

type Cacheable interface {
	// ID returns the identifier of the information.
	ID() string

	// IsOutdated checks if their's a newer version of the Cacheable.
	IsOutdated() (bool, error)

	// Discard tells the Cacheable to clean up itself.
	Discard() error
}

Cacheable defines the interface for all cacheable information.

type CacheableLoader

type CacheableLoader func(id string) (Cacheable, error)

CacheableLoader allows the user to define a function for loading/reloading of cacheable instances.

func NewFileLoader

func NewFileLoader(root string, maxSize int64) CacheableLoader

NewFileLoader returns a CacheableLoader for files. It starts at the given root directory.

type FileCacheable

type FileCacheable interface {
	Cacheable

	// ReadCloser returns the io.ReadCloser for the
	// cached file or the file itself if it's too large.
	ReadCloser() (io.ReadCloser, error)
}

FileCacheable contains a file.

type Info

type Info struct {
	ID       string
	Interval time.Duration
	TTL      time.Duration
	Len      int
}

Info contains statistical information about the Cache.

type Option

type Option func(c Cache) error

Option allows to configure a Cache.

func ID

func ID(id string) Option

ID returns the option to set the cache ID.

func Interval

func Interval(d time.Duration) Option

Interval returns the option to set the cleanup check interval.

func Loader

func Loader(l CacheableLoader) Option

Loader returns the option to set the loader function.

func TTL

func TTL(d time.Duration) Option

TTL returns the option to set the time to live for Cacheables.

Jump to

Keyboard shortcuts

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