Documentation ¶
Index ¶
- Constants
- Variables
- func Exp(ttl time.Duration) time.Time
- func Never() time.Time
- type Cache
- func (c *Cache) Cap() int
- func (c *Cache) Evict(keys ...interface{}) (size int)
- func (c *Cache) Get(k interface{}) (v interface{}, exp time.Time, err error)
- func (c *Cache) Metrics() (m Metrics)
- func (c *Cache) Set(k, v interface{}, exp time.Time) error
- func (c *Cache) Size() (n int)
- func (c *Cache) Trim(now time.Time) (expired []interface{})
- type EvictionPolicy
- type FIFO
- type Interface
- type LFU
- type LRU
- type Metrics
- type TTL
- type Upstream
- type UpstreamFunc
Constants ¶
const DefaultLFUQueueSize = 100
const DefaultLRUQueueSize = 100
Variables ¶
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 ¶
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
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
Evict removes items from the cache. It returns the new cache size.
func (*Cache) Get ¶
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) Set ¶ added in v0.1.0
Set assigns a value to a key and sets the expiration time If the size limit is reached it returns MaxSizeError.
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.
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 }
type LRU ¶ added in v0.1.0
type LRU struct { *Cache // contains filtered or unexported fields }
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