LRUCache.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  7. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. // https://medium.com/dsinjs/implementing-lru-cache-in-javascript-94ba6755cda9
  10. var Node = /*#__PURE__*/_createClass(function Node(key, value) {
  11. var next = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  12. var prev = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  13. _classCallCheck(this, Node);
  14. this.key = key;
  15. this.value = value;
  16. this.next = next;
  17. this.prev = prev;
  18. });
  19. var LRUCache = /*#__PURE__*/function () {
  20. //set default limit of 10 if limit is not passed.
  21. function LRUCache() {
  22. var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10;
  23. _classCallCheck(this, LRUCache);
  24. this.size = 0;
  25. this.limit = limit;
  26. this.head = null;
  27. this.tail = null;
  28. this.cache = {};
  29. } // Write Node to head of LinkedList
  30. // update cache with Node key and Node reference
  31. _createClass(LRUCache, [{
  32. key: "put",
  33. value: function put(key, value) {
  34. this.ensureLimit();
  35. if (!this.head) {
  36. this.head = this.tail = new Node(key, value);
  37. } else {
  38. var node = new Node(key, value, this.head);
  39. this.head.prev = node;
  40. this.head = node;
  41. } //Update the cache map
  42. this.cache[key] = this.head;
  43. this.size++;
  44. } // Read from cache map and make that node as new Head of LinkedList
  45. }, {
  46. key: "get",
  47. value: function get(key) {
  48. if (this.cache[key]) {
  49. var value = this.cache[key].value; // node removed from it's position and cache
  50. this.remove(key); // write node again to the head of LinkedList to make it most recently used
  51. this.put(key, value);
  52. return value;
  53. }
  54. console.log("Item not available in cache for key ".concat(key));
  55. }
  56. }, {
  57. key: "ensureLimit",
  58. value: function ensureLimit() {
  59. if (this.size === this.limit) {
  60. this.remove(this.tail.key);
  61. }
  62. }
  63. }, {
  64. key: "remove",
  65. value: function remove(key) {
  66. var node = this.cache[key];
  67. if (node.prev !== null) {
  68. node.prev.next = node.next;
  69. } else {
  70. this.head = node.next;
  71. }
  72. if (node.next !== null) {
  73. node.next.prev = node.prev;
  74. } else {
  75. this.tail = node.prev;
  76. }
  77. delete this.cache[key];
  78. this.size--;
  79. }
  80. }, {
  81. key: "clear",
  82. value: function clear() {
  83. this.head = null;
  84. this.tail = null;
  85. this.size = 0;
  86. this.cache = {};
  87. } // // Invokes the callback function with every node of the chain and the index of the node.
  88. // forEach(fn) {
  89. // let node = this.head;
  90. // let counter = 0;
  91. // while (node) {
  92. // fn(node, counter);
  93. // node = node.next;
  94. // counter++;
  95. // }
  96. // }
  97. // // To iterate over LRU with a 'for...of' loop
  98. // *[Symbol.iterator]() {
  99. // let node = this.head;
  100. // while (node) {
  101. // yield node;
  102. // node = node.next;
  103. // }
  104. // }
  105. }]);
  106. return LRUCache;
  107. }();
  108. exports["default"] = LRUCache;
  109. //# sourceMappingURL=LRUCache.js.map