Cache를 적용하기전 미리 결정한 체크리스트는 다음과 같습니다.
캐싱을 적용하기 위해 두 개의 주요 클래스를 사용하였습니다: LRUNode
와 LRUCache
.
class LRUNode {
key: string;
value: any;
prev: LRUNode | null;
next: LRUNode | null;
constructor(key: string, value: any) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}
LRUNode
는 캐시에 저장되는 각 데이터 항목을 나타내며, 각 노드는 key
, value
, prev
(이전 노드), next
(다음 노드)를 속성으로 가집니다.export default class LRUCache {
capacity: number;
hashmap: { [key: string]: LRUNode };
head: LRUNode;
tail: LRUNode;
constructor(capacity: number) {
this.capacity = capacity;
this.hashmap = {};
this.head = new LRUNode('0', 0);
this.tail = new LRUNode('0', 0);
this.head.next = this.tail;
this.tail.prev = this.head;
}
...
}
LRUCache
는 실제 캐시를 나타내며, head
와 tail
로 이루어진 더블 링크드 리스트와 hashmap
으로 이루어져 있습니다.