cache

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2017 License: MIT Imports: 6 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 (
	// KeyError is returned by Get if a key is not found in the cache.
	KeyError     = errors.New("Key not found.")
	ExpiredError = errors.New("Key expired.")
	// MaxSizeError is returned by Set on implementations of Interface that have max size limits.
	MaxSizeError = 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) 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() 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) []interface{}

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

type EvictionPolicy added in v0.2.0

type EvictionPolicy int
const (
	PolicyNone EvictionPolicy = iota
	PolicyFIFO
	PolicyLRU
	PolicyLFU
)

type FIFO added in v0.1.0

type FIFO struct {
	// 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) Metrics added in v0.1.0

func (c *FIFO) Metrics() Metrics

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 KeyError 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 {
	// 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) Metrics added in v0.1.0

func (c *LFU) Metrics() Metrics

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 {
	// contains filtered or unexported fields
}

func NewLRU added in v0.1.0

func NewLRU(size int) *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) Metrics added in v0.1.0

func (c *LRU) Metrics() Metrics

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{}

type Metrics added in v0.1.0

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

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

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