Skip to content

usetheplatform/lru-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

LRU Cache

Get Started

Create a cache

// Initialize a cache of size 3 that can hold payload of type V associated with key of type K
// where:
//  K = string
//  V = string
c := cache.NewCache[string, string](3)

Store a value in the cache

c.Set("key", "value")

Retreive a value from the cache

c.Get("key") // type *V or nil, if not found

Some other supported operations, that may be helpful

Check if there is such key in the cache

c.Has("key") // boolean

Check the current length of the cache

c.Length() // uint

Turn the list into an array of type *Node[K, V]

c.Items() // []*Node[K, V]

Internal types

type Cache[K comparable, V any] struct {
	capacity uint
	length   uint
	head     *Node[K, V]
}

type Node[K comparable, V any] struct {
	key     K
	payload V
	prev    *Node[K, V]
	next    *Node[K, V]
}

// Type Constructor
func NewCache[K comparable, V any](capacity uint) Cache[K, V] {
	return Cache[K, V]{
		capacity: capacity,
		length:   0,
		head:     nil,
	}
}

About

LRU Cache implementation in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages