Documentation
¶
Overview ¶
Package libcache provides in-memory caches based on different caches replacement algorithms.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // Load returns key value. Load(key interface{}) (interface{}, bool) // Peek returns key value without updating the underlying "recent-ness". Peek(key interface{}) (interface{}, bool) // Update the key value without updating the underlying "recent-ness". Update(key interface{}, value interface{}) // Store sets the key value. Store(key interface{}, value interface{}) // StoreWithTTL sets the key value with TTL overrides the default. StoreWithTTL(key interface{}, value interface{}, ttl time.Duration) // Delete deletes the key value. Delete(key interface{}) // Expiry returns key value expiry time. Expiry(key interface{}) (time.Time, bool) // Keys return cache records keys. Keys() []interface{} // Contains Checks if a key exists in cache. Contains(key interface{}) bool // Purge Clears all cache entries. Purge() // Resize cache, returning number evicted Resize(int) int // Len Returns the number of items in the cache. Len() int // Cap Returns the cache capacity. Cap() int // TTL returns entries default TTL. TTL() time.Duration // SetTTL sets entries default TTL. SetTTL(time.Duration) // RegisterOnEvicted registers a function, // to call in its own goroutine when an entry is purged from the cache. RegisterOnEvicted(f func(key, value interface{})) // RegisterOnExpired registers a function, // to call in its own goroutine when an entry TTL elapsed. // invocation of f, does not mean the entry is purged from the cache, // if need be, it must coordinate with the cache explicitly. // // var cache cache.Cache // onExpired := func(key, value interface{}) { // _, _, _ = cache.Peek(key) // } // // This should not be done unless the cache thread-safe. RegisterOnExpired(f func(key, value interface{})) }
Cache stores data so that future requests for that data can be served faster.
type ReplacementPolicy ¶
type ReplacementPolicy uint
ReplacementPolicy identifies a cache replacement policy function that implemented in another package.
const ( // IDLE cache replacement policy. IDLE ReplacementPolicy = iota + 1 // FIFO cache replacement policy. FIFO // LIFO cache replacement policy. LIFO // LRU cache replacement policy. LRU // LFU cache replacement policy. LFU // MRU cache replacement policy. MRU // ARC cache replacement policy. ARC )
func (ReplacementPolicy) Available ¶
func (c ReplacementPolicy) Available() bool
Available reports whether the given cache replacement policy is linked into the binary.
func (ReplacementPolicy) New ¶
func (c ReplacementPolicy) New(cap int) Cache
New returns a new thread safe cache. New panics if the cache replacement policy function is not linked into the binary.
func (ReplacementPolicy) NewUnsafe ¶
func (c ReplacementPolicy) NewUnsafe(cap int) Cache
NewUnsafe returns a new non-thread safe cache. NewUnsafe panics if the cache replacement policy function is not linked into the binary.
func (ReplacementPolicy) Register ¶
func (c ReplacementPolicy) Register(function func(cap int) Cache)
Register registers a function that returns a new cache instance, of the given cache replacement policy function. This is intended to be called from the init function, in packages that implement cache replacement policy function.
func (ReplacementPolicy) String ¶
func (c ReplacementPolicy) String() string
String returns string describes the cache replacement policy function.
Directories
¶
Path | Synopsis |
---|---|
Package arc implements an ARC cache.
|
Package arc implements an ARC cache. |
Package fifo implements an FIFO cache.
|
Package fifo implements an FIFO cache. |
Package idle implements an IDLE cache, that never finds/stores a key's value.
|
Package idle implements an IDLE cache, that never finds/stores a key's value. |
Package lfu implements an LFU cache.
|
Package lfu implements an LFU cache. |
Package lifo implements an LIFO cache.
|
Package lifo implements an LIFO cache. |
Package lru implements an LRU cache.
|
Package lru implements an LRU cache. |
Package mru implements an MRU cache.
|
Package mru implements an MRU cache. |