cache

package
v0.0.0-...-2e8e066 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package cache provides a generic threadsafe expiring cache implementation that is capable of passing cache misses through to a lookup function.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is returned from calls to the cache or in the event that the
	// Close() function has already been called.
	ErrClosed = errors.New("the cache is closing or already closed")
)

Functions

This section is empty.

Types

type Cache

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

Cache is a threadsafe expiring cache that is capable of passing cache misses through to a lookup function.

func New

func New(duration time.Duration, lookup Lookup) *Cache

New returns a new cache whose values survive for the given duration and are retrieved with the given lookup function.

func (*Cache) Close

func (cache *Cache) Close()

Close will release any resources consumed by the cache and its contents. It will also prevent further use of the cache.

func (*Cache) Evict

func (cache *Cache) Evict()

Evict will expuge all existing values from the cache. Outstanding lookups that are still pending will not be affected.

func (*Cache) Lookup

func (cache *Cache) Lookup(ctx context.Context, key Key, tracker dfsr.Tracker) (value Value, err error)

Lookup returns the value for the given key if it exists in the cache and has not expired. If the cached value is missing or expired, a lookup will be performed.

If the cache has been closed then ErrClosed will be returned.

func (*Cache) Set

func (cache *Cache) Set(key Key, value Value)

Set saves a value in the cache for the given key. If a value already exists in the cache for that key, the existing value is replaced.

If the cache has been closed then Set will do nothing.

func (*Cache) Value

func (cache *Cache) Value(key Key) (value Value, ok bool)

Value returns the value for the given key if it exists in the cache and has not expired. If the cached value is missing or expired, ok will be false.

If the cache has been closed then ok will be false.

type Closer

type Closer interface {
	Close()
}

Closer is an interface that cache values may implement to be closed when their entries expire.

type Key

type Key interface{}

Key defines a cache key.

type Lookup

type Lookup func(ctx context.Context, key Key, tracker dfsr.Tracker) (value Value, err error)

Lookup defines a lookup function for retrieving new cache values.

type Releaser

type Releaser interface {
	Release()
}

Releaser is an interface that cache values may implement to be released when their entries expire.

type Value

type Value interface{}

Value defines a cache value.

Jump to

Keyboard shortcuts

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