caching

package
v0.0.0-...-846829c Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 2 Imported by: 0

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

func CreateLRUCache(capacity uint) *LRUCache

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

func (lru *LRUCache) Add(key string, value any)

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.

func (*LRUCache) Get

func (lru *LRUCache) Get(key string) (value any, exist bool)

Get is a public function.

It will return the value of the key. If the key doesn't exist, it will return nil.

It will check if the nodeMap has the key. If the key exists, we will move the item to the front of the cache. If the key doesn't exist, we will return nil.

func (*LRUCache) Remove

func (lru *LRUCache) Remove(key string) bool

Remove is a public function.

It will return true if it was removed. It will return false if the key doesn't exist.

The Remove function will check if the given entry exist in the cache and remove the entry from the cache.

Jump to

Keyboard shortcuts

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