Now, when we new
up a Cache
instance, we'll want certain things predefined in the constructor. Let's flesh that out:
- We'll need a reference to the capacity
- A
count
for the number of keys we have - The
cache
via a hash table itself - References to the heads and tails of the doubly linked list
1class Cache {
2 constructor(capacity) {
3 this.count = 0;
4 this.capacity = capacity;
5 this.cache = {};
6 this.head = new DLinkedNode();
7 this.head.pre = null;
8 this.tail = new DLinkedNode();
9 this.tail.next = null;
10 this.head.next = this.tail;
11 this.tail.pre = this.head;
12 }
13}