Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache is the public struct.
It holds the capacity, item amount, items and a read/write lock. It has a public function for adding items. It has a public function for checking if the key exists.
A LRUCache is used to store items in a cache. It has a fixed capacity. When the capacity is reached, the last item is removed. The items are ordered in the cache, by the order they were added or accessed. The items are stored within in a Doubly linked list, and the invidual items are stored as well in map[string] for O(1) lookup.
If you access a item via the `Get` function, it will be moved to the front of the cache. This behavior doesn't exist for the `Exists` function.
func CreateLRUCache ¶
CreateLRUCache is a public function.
It takes the capacity(int) as an argument. It returns a pointer to a LRUCache.
It creates a new LRUCache struct and fills it with the necssarry values.
func (*LRUCache) Add ¶
Add is a public function.
It takes the key(string) and the value(interface{}). The function evaluates first if the key already exists. If it doesn't exist and the cache is full, we first have to remove the last item.
If the key does exist, and the index of the item is not the first item, we have to move the item to the front of the cache.
If the key doesn't exist.
func (*LRUCache) Exist ¶
func (lru *LRUCache) Exist(key string) (listNode *linkedlist.Node, exists bool)
Exist is a public function.
It will return a bool and an int. The bool will be true if the key exists. If the key exist, it will also return the node that is in the list, of the given key.