Package cache implements an inmemory cache for any interface{} object.

Although exported, this package is non intended for general consumption. It is a shared dependency between multiple exposure notifications projects. We cannot guarantee that there won't be breaking changes in the future.



This section is empty.


View Source
var ErrInvalidDuration = errors.New("expireAfter duration cannot be negative")


This section is empty.


type Cache

type Cache struct {
	// contains filtered or unexported fields

func New

func New(expireAfter time.Duration) (*Cache, error)

New creates a new in memory cache.

func (*Cache) Lookup

func (c *Cache) Lookup(name string) (interface{}, bool)

Lookup checks the cache for a non-expired object by the supplied key name. The bool return informs the caller if there was a cache hit or not. A return of nil, true means that nil is in the cache. Where nil, false indicates a cache miss or that the value is expired and should be refreshed.

func (*Cache) Set

func (c *Cache) Set(name string, object interface{}) error

Set saves the current value of an object in the cache, with the supplied durintion until the object expires.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of items in the cache.

func (*Cache) WriteThruLookup

func (c *Cache) WriteThruLookup(name string, primaryLookup Func) (interface{}, error)

WriteThruLookup checks the cache for the value associated with name, and if not found or expired, invokes the provided primaryLookup function to local the value.

type Func

type Func func() (interface{}, error)

Source Files