cache

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package cache provides LRU cache map functionality.

Index

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

func NewMap(list *MapItem) *Map

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) Delete

func (cmap *Map) Delete(key string)

Delete deletes an item by key.

func (*Map) Expire

func (cmap *Map) Expire(fn func(list, item *MapItem) bool)

Expire performs list item expiration using a helper function.

See MapItem.Expire for a full discussion.

func (*Map) Get

func (cmap *Map) Get(key string) (*MapItem, bool)

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

func (cmap *Map) InitMap(list *MapItem)

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.

func (*Map) Items

func (cmap *Map) Items() map[string]*MapItem

Items returns the internal map of the cache map.

func (*Map) Set

func (cmap *Map) Set(key string, newitem *MapItem, expirable bool)

Set sets an item by key.

Whether the new item can be expired is controlled by the expirable parameter. Expirable items are tracked in an LRU 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) Empty

func (item *MapItem) Empty()

Empty initializes the list item as empty.

func (*MapItem) Expire

func (list *MapItem) Expire(fn func(list, item *MapItem) bool)

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

func (item *MapItem) InsertHead(list *MapItem)

InsertHead inserts the list item to the head of a list.

func (*MapItem) InsertTail

func (item *MapItem) InsertTail(list *MapItem)

InsertTail inserts the list item to the tail of a list.

func (*MapItem) IsEmpty

func (item *MapItem) IsEmpty() bool

IsEmpty determines if the list item is empty.

func (*MapItem) Iterate

func (list *MapItem) Iterate(fn func(list, item *MapItem) bool)

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.

func (*MapItem) Remove

func (item *MapItem) Remove()

Remove removes the list item from any list it is in.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL