INNER CODE UNIT · Go

NewLRUCache

duke-git/lancet · algorithm/lrucache.go:30

func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] {
	return &LRUCache[K, V]{
		cache:    make(map[K]*lruNode[K, V], capacity),
		head:     nil,
		tail:     nil,
		capacity: capacity,
		length:   0,
	}
}

// Get value of key from lru cache.
// Play: https://go.dev/play/p/iUynEfOP8G0
func (l *LRUCache[K, V]) Get(key K) (V, bool) {
	var value V

	node, ok := l.cache[key]
	if ok {
		l.moveToTail(node)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…