-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from sonroyaalmerol/io-speed
fix io speed and io total labeling
- Loading branch information
Showing
6 changed files
with
155 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package utils | ||
|
||
import ( | ||
"container/list" | ||
) | ||
|
||
type entry struct { | ||
key string | ||
value interface{} | ||
} | ||
|
||
type LRUCache struct { | ||
capacity int | ||
cache map[string]*list.Element | ||
list *list.List | ||
} | ||
|
||
func NewLRUCache(capacity int) *LRUCache { | ||
return &LRUCache{ | ||
capacity: capacity, | ||
cache: make(map[string]*list.Element), | ||
list: list.New(), | ||
} | ||
} | ||
|
||
func (l *LRUCache) Get(key string) (interface{}, bool) { | ||
if elem, found := l.cache[key]; found { | ||
l.list.MoveToFront(elem) | ||
return elem.Value.(*entry).value, true | ||
} | ||
return nil, false | ||
} | ||
|
||
func (l *LRUCache) Set(key string, value interface{}) { | ||
if elem, found := l.cache[key]; found { | ||
l.list.MoveToFront(elem) | ||
elem.Value.(*entry).value = value | ||
return | ||
} | ||
|
||
if l.list.Len() >= l.capacity { | ||
oldest := l.list.Back() | ||
if oldest != nil { | ||
oldestEntry := oldest.Value.(*entry) | ||
delete(l.cache, oldestEntry.key) | ||
l.list.Remove(oldest) | ||
} | ||
} | ||
|
||
newEntry := &entry{key: key, value: value} | ||
frontElem := l.list.PushFront(newEntry) | ||
l.cache[key] = frontElem | ||
} |