LRUCache.js 3.8 KB

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