LRUCache.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // https://medium.com/dsinjs/implementing-lru-cache-in-javascript-94ba6755cda9
  2. class Node {
  3. constructor(key, value, next = null, prev = null) {
  4. this.key = key;
  5. this.value = value;
  6. this.next = next;
  7. this.prev = prev;
  8. }
  9. }
  10. export default class LRUCache {
  11. //set default limit of 10 if limit is not passed.
  12. constructor(limit = 10) {
  13. this.size = 0;
  14. this.limit = limit;
  15. this.head = null;
  16. this.tail = null;
  17. this.cache = {};
  18. }
  19. // Write Node to head of LinkedList
  20. // update cache with Node key and Node reference
  21. put(key, value){
  22. this.ensureLimit();
  23. if(!this.head){
  24. this.head = this.tail = new Node(key, value);
  25. }else{
  26. const node = new Node(key, value, this.head);
  27. this.head.prev = node;
  28. this.head = node;
  29. }
  30. //Update the cache map
  31. this.cache[key] = this.head;
  32. this.size++;
  33. }
  34. // Read from cache map and make that node as new Head of LinkedList
  35. get(key){
  36. if(this.cache[key]){
  37. const value = this.cache[key].value;
  38. // node removed from it's position and cache
  39. this.remove(key)
  40. // write node again to the head of LinkedList to make it most recently used
  41. this.put(key, value);
  42. return value;
  43. }
  44. console.log(`Item not available in cache for key ${key}`);
  45. }
  46. ensureLimit(){
  47. if(this.size === this.limit){
  48. this.remove(this.tail.key)
  49. }
  50. }
  51. remove(key){
  52. const node = this.cache[key];
  53. if(node.prev !== null){
  54. node.prev.next = node.next;
  55. }else{
  56. this.head = node.next;
  57. }
  58. if(node.next !== null){
  59. node.next.prev = node.prev;
  60. }else{
  61. this.tail = node.prev
  62. }
  63. delete this.cache[key];
  64. this.size--;
  65. }
  66. clear() {
  67. this.head = null;
  68. this.tail = null;
  69. this.size = 0;
  70. this.cache = {};
  71. }
  72. // // Invokes the callback function with every node of the chain and the index of the node.
  73. // forEach(fn) {
  74. // let node = this.head;
  75. // let counter = 0;
  76. // while (node) {
  77. // fn(node, counter);
  78. // node = node.next;
  79. // counter++;
  80. // }
  81. // }
  82. // // To iterate over LRU with a 'for...of' loop
  83. // *[Symbol.iterator]() {
  84. // let node = this.head;
  85. // while (node) {
  86. // yield node;
  87. // node = node.next;
  88. // }
  89. // }
  90. }