fix: concurrent write on LRU cache (#200)

This commit is contained in:
harttle
2020-03-10 01:09:41 +08:00
parent 06ad0481db
commit 6de933897e
3 changed files with 69 additions and 13 deletions
+10 -6
View File
@@ -25,13 +25,17 @@ export class LRU<T> implements Cache<T> {
}
write (key: string, value: T) {
const node = new Node(key, value, this.head.next, this.head)
this.head.next.prev = node
this.head.next = node
if (this.cache[key]) {
this.cache[key].value = value
} else {
const node = new Node(key, value, this.head.next, this.head)
this.head.next.prev = node
this.head.next = node
this.cache[key] = node
this.size++
this.ensureLimit()
this.cache[key] = node
this.size++
this.ensureLimit()
}
}
read (key: string): T | undefined {