Documentation
¶
Index ¶
- Constants
- Variables
- func Exp(ttl time.Duration) *time.Time
- func Never() *time.Time
- type Cache
- 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() int
- func (c *Cache) Trim(now time.Time) []interface{}
- type EvictionPolicy
- type FIFO
- type Interface
- type LFU
- type LRU
- type Metrics
- type Upstream
- type UpstreamFunc
Constants ¶
const DefaultLFUQueueSize = 100
const DefaultLRUQueueSize = 100
Variables ¶
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 ¶
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 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.
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
}
type LRU ¶ added in v0.1.0
type LRU struct {
// contains filtered or unexported fields
}