Documentation ¶
Overview ¶
Package cache provides LRU cache map functionality.
Index ¶
- type Map
- type MapItem
- func (item *MapItem) Empty()
- func (list *MapItem) Expire(fn func(list, item *MapItem) bool)
- func (item *MapItem) InsertHead(list *MapItem)
- func (item *MapItem) InsertTail(list *MapItem)
- func (item *MapItem) IsEmpty() bool
- func (list *MapItem) Iterate(fn func(list, item *MapItem) bool)
- func (item *MapItem) Remove()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a map of key/value pairs that also maintains its items in an LRU (Least Recently Used) list. LRU items may then be expired.
func NewMap ¶
NewMap creates a new cache map.
The cache map tracks items in the LRU list specified by the list parameter. If the list parameter is nil then items are tracked in an internal list.
func (*Map) Expire ¶
Expire performs list item expiration using a helper function.
See MapItem.Expire for a full discussion.
func (*Map) Get ¶
Get gets an item by key.
Get "touches" the item to show that it was recently used. For this reason Get modifies the internal structure of the cache map and is not safe to be called under a read lock.
func (*Map) InitMap ¶ added in v0.2.0
InitMap initializes a zero cache map.
The cache map tracks items in the LRU list specified by the list parameter. If the list parameter is nil then items are tracked in an internal list.
type MapItem ¶
type MapItem struct { Value interface{} // contains filtered or unexported fields }
MapItem is the data structure that is stored in a Map.
func (*MapItem) Expire ¶
Expire performs list item expiration using a helper function.
Expire iterates over the list and calls the helper function fn() on every list item. The function fn() must perform an expiration test on the list item and perform one of the following:
- If the list item is not expired, fn() must return false. Expire will then stop the loop iteration.
- If the list item is expired, fn() has two options. It may remove the item by using item.Remove() (item eviction). Or it may remove the item by using item.Remove() and reinsert the item at the list tail using item.InsertTail(list) (item refresh). In this second case care must be taken to ensure that fn() returns false for some item in the list; otherwise the Expire iteration will continue forever, because the list will never be found empty.
func (*MapItem) InsertHead ¶
InsertHead inserts the list item to the head of a list.
func (*MapItem) InsertTail ¶
InsertTail inserts the list item to the tail of a list.
func (*MapItem) Iterate ¶
Iterate iterates over the list using a helper function.
Iterate iterates over the list and calls the helper function fn() on every list item. The function fn() must not modify the list in any way. The function fn() must return true to continue the iteration and false to stop it.