File tree 1 file changed +11
-11
lines changed
1 file changed +11
-11
lines changed Original file line number Diff line number Diff line change @@ -10,19 +10,19 @@ type item struct {
10
10
}
11
11
12
12
type LRU struct {
13
- dl * linkedlist.Doubly [any ]
14
- capacity int
15
- maxCapacity int
16
- storage map [string ]* linkedlist.Node [any ]
13
+ dl * linkedlist.Doubly [any ]
14
+ size int
15
+ capacity int
16
+ storage map [string ]* linkedlist.Node [any ]
17
17
}
18
18
19
19
// NewLRU represent initiate lru cache with capacity
20
20
func NewLRU (capacity int ) LRU {
21
21
return LRU {
22
- dl : linkedlist .NewDoubly [any ](),
23
- storage : make (map [string ]* linkedlist.Node [any ], capacity ),
24
- capacity : 0 ,
25
- maxCapacity : capacity ,
22
+ dl : linkedlist .NewDoubly [any ](),
23
+ storage : make (map [string ]* linkedlist.Node [any ], capacity ),
24
+ size : 0 ,
25
+ capacity : capacity ,
26
26
}
27
27
}
28
28
@@ -49,17 +49,17 @@ func (c *LRU) Put(key string, value any) {
49
49
return
50
50
}
51
51
52
- if c .capacity >= c .maxCapacity {
52
+ if c .size >= c .capacity {
53
53
e := c .dl .Front ()
54
54
dk := e .Val .(item ).key
55
55
c .dl .Remove (e )
56
56
delete (c .storage , dk )
57
- c .capacity --
57
+ c .size --
58
58
}
59
59
60
60
n := item {key : key , value : value }
61
61
c .dl .AddAtEnd (n )
62
62
ne := c .dl .Back ()
63
63
c .storage [key ] = ne
64
- c .capacity ++
64
+ c .size ++
65
65
}
You can’t perform that action at this time.
0 commit comments