* Sets the value to cache by its key. * Time complexity: O(1) in average. * @param {string} key * @param {any} val
(key, val)
| 61 | * @param {any} val |
| 62 | */ |
| 63 | set(key, val) { |
| 64 | if (this.nodesMap[key]) { |
| 65 | const node = this.nodesMap[key]; |
| 66 | node.val = val; |
| 67 | this.promote(node); |
| 68 | } else { |
| 69 | const node = new LinkedListNode(key, val); |
| 70 | this.append(node); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Promotes the node to the end of the linked list. |
no test coverage detected