Documentation ¶
Overview ¶
Package lfuda provides a Least Frequently Used with Dynamic Aging cache
In addition to basic LFU functionality it behaves according to the following logic: - The cache dynamically "ages" through a global "age" counter - Every cache eviction sets the global "age" counter to the evicted item's hits counter, - When setting a new item, its "hits" counter should be set to the cache's "age" value - When an existing item is updated, its "hits" counter is incremented by 1 to at least "age" + 1.
The cache in this package take locks while operating. Its therefore thread-safe and can be used with multiple goroutines
For use with a single goroutine (to avoid the locking overhead), the simplelfuda package can be used
Index ¶
- type Cache
- func New(size float64) *Cache
- func NewGDSF(size float64) *Cache
- func NewGDSFWithEvict(size float64, onEvicted func(key interface{}, value interface{})) *Cache
- func NewLFU(size float64) *Cache
- func NewLFUWithEvict(size float64, onEvicted func(key interface{}, value interface{})) *Cache
- func NewWithEvict(size float64, onEvicted func(key interface{}, value interface{})) *Cache
- func (c *Cache) Age() (age float64)
- func (c *Cache) Contains(key interface{}) bool
- func (c *Cache) ContainsOrSet(key, value interface{}) (ok, set bool)
- func (c *Cache) Get(key interface{}) (value interface{}, ok bool)
- func (c *Cache) Keys() []interface{}
- func (c *Cache) Len() (length int)
- func (c *Cache) Peek(key interface{}) (value interface{}, ok bool)
- func (c *Cache) PeekOrSet(key, value interface{}) (previous interface{}, ok, set bool)
- func (c *Cache) Purge()
- func (c *Cache) Remove(key interface{}) (present bool)
- func (c *Cache) Set(key, value interface{}) (ok bool)
- func (c *Cache) Size() (size float64)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe fixed size lfuda cache.
func NewGDSF ¶ added in v0.3.0
NewGDSF creates an lfuda of the given size and the GDSF cache policy.
func NewGDSFWithEvict ¶ added in v0.3.0
NewGDSFWithEvict constructs a fixed GDSF size cache with the given eviction callback.
func NewLFUWithEvict ¶ added in v0.3.0
NewLFUWithEvict constructs a fixed size LFU cache with the given eviction callback.
func NewWithEvict ¶
NewWithEvict constructs a fixed size LFUDA cache with the given eviction callback.
func (*Cache) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*Cache) ContainsOrSet ¶
ContainsOrSet checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether the key/value was set or not.
func (*Cache) Keys ¶
func (c *Cache) Keys() []interface{}
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*Cache) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*Cache) PeekOrSet ¶
PeekOrSet checks if a key is in the cache without updating the hits or deleting it for being stale, and if not, adds the value. Returns whether found and whether the key/value was set or not.