cache

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stoppable

type Stoppable interface {
	// Stop is the method called when an item is evicted from the cache.
	Stop()
}

Stoppable is the interface that wraps a basic Stop method.

type TTL

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

TTL is a time-to-live cache. Entries added to it are automatically culled after a period of inactivity. When evicted from the cache, the entry's Stop() method will be called.

Example usage:

cache := cache.NewTTL()
go cache.Start(stopCh)
cachedValue := cache.Insert(key, value)
cachedValue.Start()
otherValue := cache.Get(otherKey)
if otherValue != nil {
    ...use otherValue...
}
...cachedValue times out and cachedValue.Stop() is called in a background goroutine...

func NewTTL

func NewTTL() *TTL

NewTTL creates a new cache.TTL. Note that for the culling to occur, Start(stopCh) must be called.

cache := cache.NewTTL()
go cache.Start(stopCh)

func (*TTL) DeleteIfPresent

func (c *TTL) DeleteIfPresent(keyString string, value Stoppable)

DeleteIfPresent deletes an entry from the cache if its value in the cache is equal to value.

func (*TTL) Get

func (c *TTL) Get(keyString string) Stoppable

Get gets an item in the cache and extends its time to live. If the key is not present in the cache, nil is returned.

func (*TTL) Insert

func (c *TTL) Insert(keyString string, value Stoppable) Stoppable

Insert inserts an item into the cache. If something is already stored with that key, then the existing stored value is returned. This is especially important if the value needs to be started, in which case the returned value must be started, not the input.

cachedValue := cache.Insert(key, value)
// Note that cachedValue may either be value or something that was already in the cache.
cachedValue.Start()

func (*TTL) Start

func (c *TTL) Start(stopCh <-chan struct{}) error

Start continuously culls the old items from the cache. It should be run in a goroutine.

Jump to

Keyboard shortcuts

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