cache

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2017 License: MIT Imports: 7 Imported by: 0

README

go-cache

Cache structs for Go

Basic cache

Basic implements Interface that stores an unlimited number of items. It has no eviction policy removal of expired items is the responsibility of the user (via Basic#Trim(time.Time)).

Documentation

Index

Constants

View Source
const DefaultLFUQueueSize = 100
View Source
const DefaultLRUQueueSize = 100

Variables

View Source
var (
	// ErrKeyNotFound is returned by Get if a key is not found in the cache.
	ErrKeyNotFound = errors.New("Key not found.")
	ErrExpired     = errors.New("Key expired.")
	// ErrMaxSize is returned by Set on implementations of Interface that have max size limits.
	ErrMaxSize = errors.New("Cache full.")
)

Functions

func Exp added in v0.1.0

func Exp(ttl time.Duration) time.Time

func Never added in v0.1.0

func Never() time.Time

Never is a helper that returns a nil expiration time.

Types

type Cache

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

Cache implements Interface. Removal of expired items is the responsibility of the caller.

func New added in v0.1.0

func New(size int) *Cache

New returns a new Cache. size determines the maximum number of items the cache can hold. If set to zero or less the cache will not have a size limit.

func (*Cache) Cap added in v0.3.0

func (c *Cache) Cap() int

Size returns size of all keys in cache both expired and fresh

func (*Cache) Evict added in v0.1.0

func (c *Cache) Evict(keys ...interface{}) (size int)

Evict removes items from the cache. It returns the new cache size.

func (*Cache) Get

func (c *Cache) Get(k interface{}) (v interface{}, exp time.Time, err error)

Get returns a value assigned to a key and it's expiration time. If a key does not exist in cache KeyError is returned. If a key is expired ExpiredError is returned

func (*Cache) Metrics added in v0.1.0

func (c *Cache) Metrics() (m Metrics)

func (*Cache) Set added in v0.1.0

func (c *Cache) Set(k, v interface{}, exp time.Time) error

Set assigns a value to a key and sets the expiration time If the size limit is reached it returns MaxSizeError.

func (*Cache) Size added in v0.1.0

func (c *Cache) Size() (n int)

Size returns size of all keys in cache both expired and fresh

func (*Cache) Trim added in v0.1.0

func (c *Cache) Trim(now time.Time) (expired []interface{})

Trim removes all expired keys and returns a slice of removed keys

type EvictionPolicy added in v0.2.0

type EvictionPolicy string
const (
	PolicyNone EvictionPolicy = ""
	PolicyFIFO EvictionPolicy = "FIFO"
	PolicyLRU  EvictionPolicy = "LRU"
	PolicyLFU  EvictionPolicy = "LFU"
	PolicyTTL  EvictionPolicy = "TTL"
)

type FIFO added in v0.1.0

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

FIFO implements Interface with a first-in-first-out eviction policy.

func NewFIFO added in v0.1.0

func NewFIFO(size int) *FIFO

func (*FIFO) Evict added in v0.1.0

func (c *FIFO) Evict(keys ...interface{}) int

func (*FIFO) Get added in v0.2.0

func (c *FIFO) Get(k interface{}) (v interface{}, exp time.Time, err error)

func (*FIFO) Set added in v0.1.0

func (c *FIFO) Set(k, v interface{}, exp time.Time) (err error)

Set assigns a value to a key and sets the expiration time. If the size limit is reached the oldest item stored is evicted to insert the new one

func (*FIFO) Trim added in v0.1.0

func (c *FIFO) Trim(now time.Time) []interface{}

type Interface added in v0.1.0

type Interface interface {
	// Interface implements Upstream and returns ErrKeyNotFound if a key is not found in cache.
	Upstream
	// Set assigns a value to a key and sets the expiration time
	Set(key, value interface{}, exp time.Time) error
	// Evict drops the provided keys from cache (if they exist) and returns the new size of the cache.
	// Calling Evict without arguments returns the current cache size.
	Evict(keys ...interface{}) (size int)
	Metrics() Metrics
}

Interface is the common cache interface for all implementations.

func NewCache added in v0.2.0

func NewCache(size int, policy EvictionPolicy) Interface

type LFU added in v0.1.0

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

func NewLFU added in v0.1.0

func NewLFU(size int) *LFU

func (*LFU) Evict added in v0.1.0

func (c *LFU) Evict(keys ...interface{}) int

func (*LFU) Flush added in v0.1.0

func (c *LFU) Flush()

func (*LFU) Get added in v0.1.0

func (c *LFU) Get(x interface{}) (y interface{}, exp time.Time, err error)

func (*LFU) Set added in v0.1.0

func (c *LFU) Set(x, y interface{}, exp time.Time) (err error)

func (*LFU) Trim added in v0.1.0

func (c *LFU) Trim(now time.Time) []interface{}

type LRU added in v0.1.0

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

func NewLRU added in v0.1.0

func NewLRU(size int) (c *LRU)

func (*LRU) Evict added in v0.1.0

func (c *LRU) Evict(keys ...interface{}) int

func (*LRU) Flush added in v0.1.0

func (c *LRU) Flush()

func (*LRU) Get added in v0.1.0

func (c *LRU) Get(x interface{}) (y interface{}, exp time.Time, err error)

func (*LRU) Set added in v0.1.0

func (c *LRU) Set(x, y interface{}, exp time.Time) (err error)

func (*LRU) Trim added in v0.1.0

func (c *LRU) Trim(now time.Time) []interface{}

Trim removes expired pairs from the cache and LRU list

type Metrics added in v0.1.0

type Metrics struct {
	Hit, Miss, Evict, Expired, Items uint64
}

type TTL added in v0.2.4

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

TTL implements Interface with eviction of sooner-to-expire elements

func NewTTL added in v0.2.4

func NewTTL(size int) *TTL

func (*TTL) Evict added in v0.2.4

func (c *TTL) Evict(keys ...interface{}) (n int)

func (*TTL) Get added in v0.2.4

func (c *TTL) Get(k interface{}) (v interface{}, exp time.Time, err error)

func (*TTL) Set added in v0.2.4

func (c *TTL) Set(x, y interface{}, exp time.Time) (err error)

Set assigns a value to a key and sets the expiration time. If the size limit is reached the oldest item stored is evicted to insert the new one

func (*TTL) Trim added in v0.2.4

func (c *TTL) Trim(now time.Time) []interface{}

type Upstream

type Upstream interface {
	Get(x interface{}) (y interface{}, exp time.Time, err error)
}

func Blocking

func Blocking(up Upstream) Upstream

Blocking avoids multiple simultaneous requests for the same key

func Proxy added in v0.1.0

func Proxy(u Upstream, c Interface) Upstream

func ProxyFunc added in v0.3.1

func ProxyFunc(u UpstreamFunc, c Interface) Upstream

type UpstreamFunc

type UpstreamFunc func(x interface{}) (y interface{}, exp time.Time, err error)

func (UpstreamFunc) Get added in v0.1.0

func (f UpstreamFunc) Get(x interface{}) (y interface{}, exp time.Time, err error)

Jump to

Keyboard shortcuts

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