MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / Put

Method Put

cache/lru.go:52–75  ·  view source on GitHub ↗

Put cache with key and value to lru

(key string, value any)

Source from the content-addressed store, hash-verified

50
51// Put cache with key and value to lru
52func (c *LRU) Put(key string, value any) {
53 e, ok := c.storage[key]
54 if ok {
55 n := e.Val.(item)
56 n.value = value
57 e.Val = n
58 c.dl.MoveToBack(e)
59 return
60 }
61
62 if c.size >= c.capacity {
63 e := c.dl.Front()
64 dk := e.Val.(item).key
65 c.dl.Remove(e)
66 delete(c.storage, dk)
67 c.size--
68 }
69
70 n := item{key: key, value: value}
71 c.dl.AddAtEnd(n)
72 ne := c.dl.Back()
73 c.storage[key] = ne
74 c.size++
75}

Callers 1

TestLRUFunction · 0.95

Calls 5

MoveToBackMethod · 0.80
FrontMethod · 0.45
RemoveMethod · 0.45
AddAtEndMethod · 0.45
BackMethod · 0.45

Tested by 1

TestLRUFunction · 0.76