Documentation
¶
Overview ¶
Package cache implements various caching strategies.
Index ¶
- type EvictionNotifier
- type Iterator
- type LRU
- func (c *LRU) Add(key, value interface{})
- func (c *LRU) Get(key interface{}) (value interface{}, ok bool)
- func (c *LRU) Len() int
- func (c *LRU) NewIterator() *Iterator
- func (c *LRU) NewReverseIterator() *Iterator
- func (c *LRU) PeekNewest() (key, value interface{})
- func (c *LRU) PeekOldest() (key, value interface{})
- func (c *LRU) Remove(key interface{}) interface{}
- func (c *LRU) RemoveOldest() (key, value interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvictionNotifier ¶
type EvictionNotifier interface { // OnEviction is called on the value of an LRU entry when it's about // to be evicted from the cache. This method must not call the LRU // cache nor block indefinitely. OnEviction(key interface{}) }
EvictionNotifier is an optional interface used by LRU entries that wish to be notified when they are being deleted from the LRU. If implemented by the value of an LRU entry, OnEviction is called when it's time for that entry to be evicted, due to an Add. It is not called if the entry is removed by the LRU's Remove or RemoveOldest methods.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator is an iterator through the list. The iterator points to a nil element at the end of the list.
func (*Iterator) GetAndAdvance ¶
GetAndAdvance returns key, value, true if the current entry is valid and advances the iterator. Otherwise it returns nil, nil, false.
type LRU ¶
type LRU struct {
// contains filtered or unexported fields
}
LRU is a least-recently used cache, safe for concurrent access.
func (*LRU) Add ¶
func (c *LRU) Add(key, value interface{})
Add adds the provided key and value to the cache, evicting an old item if necessary.
func (*LRU) Get ¶
Get fetches the key's value from the cache. The ok result will be true if the item was found.
func (*LRU) NewIterator ¶
NewIterator returns a new iterator for the LRU.
func (*LRU) NewReverseIterator ¶
NewReverseIterator returns a new reverse iterator for the LRU.
func (*LRU) PeekNewest ¶
func (c *LRU) PeekNewest() (key, value interface{})
PeekNewest peeks at the most-recently used entry, without modifying the cache in any way. If the cache is empty, two nils are returned.
func (*LRU) PeekOldest ¶
func (c *LRU) PeekOldest() (key, value interface{})
PeekOldest peeks at the least-recently used entry, without modifying the cache in any way. If the cache is empty, two nils are returned.
func (*LRU) Remove ¶
func (c *LRU) Remove(key interface{}) interface{}
Remove removes a key from the cache. The return value is the value that was removed or nil if the key was not present. The value's EvictionNotifier is not run by Remove.
func (*LRU) RemoveOldest ¶
func (c *LRU) RemoveOldest() (key, value interface{})
RemoveOldest removes the oldest item in the cache and returns its key and value. If the cache is empty, the empty string and nil are returned. The value's EvictionNotifier is not run.