index.mjs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. const createLRU = (options) => {
  2. let { max } = options;
  3. if (!(Number.isInteger(max) && max > 0))
  4. throw new TypeError("`max` must be a positive integer");
  5. let size = 0;
  6. let head = 0;
  7. let tail = 0;
  8. let free = [];
  9. const { onEviction } = options;
  10. const keyMap = /* @__PURE__ */ new Map();
  11. const keyList = new Array(max).fill(void 0);
  12. const valList = new Array(max).fill(void 0);
  13. const next = new Array(max).fill(0);
  14. const prev = new Array(max).fill(0);
  15. const linkTail = (index) => {
  16. next[tail] = index;
  17. prev[index] = tail;
  18. next[index] = 0;
  19. tail = index;
  20. };
  21. const moveToTail = (index) => {
  22. if (index === tail) return;
  23. const nextIndex = next[index];
  24. const prevIndex = prev[index];
  25. if (index === head) head = nextIndex;
  26. else next[prevIndex] = nextIndex;
  27. prev[nextIndex] = prevIndex;
  28. linkTail(index);
  29. };
  30. const _shrink = (newMax) => {
  31. let current = tail;
  32. const preserve = Math.min(size, newMax);
  33. const remove = size - preserve;
  34. const newKeyList = new Array(preserve);
  35. const newValList = new Array(preserve);
  36. for (let i = 0; i < remove; i++) {
  37. const key = keyList[head];
  38. onEviction == null ? void 0 : onEviction(key, valList[head]);
  39. keyMap.delete(key);
  40. head = next[head];
  41. }
  42. for (let i = preserve - 1; i >= 0; i--) {
  43. newKeyList[i] = keyList[current];
  44. newValList[i] = valList[current];
  45. keyMap.set(keyList[current], i);
  46. current = prev[current];
  47. }
  48. head = 0;
  49. tail = preserve - 1;
  50. size = preserve;
  51. keyList.length = newMax;
  52. valList.length = newMax;
  53. next.length = newMax;
  54. prev.length = newMax;
  55. for (let i = 0; i < preserve; i++) {
  56. keyList[i] = newKeyList[i];
  57. valList[i] = newValList[i];
  58. next[i] = i + 1;
  59. prev[i] = i - 1;
  60. }
  61. free = [];
  62. for (let i = preserve; i < newMax; i++) free.push(i);
  63. };
  64. const _grow = (newMax) => {
  65. keyList.length = newMax;
  66. valList.length = newMax;
  67. next.length = newMax;
  68. prev.length = newMax;
  69. keyList.fill(void 0, max);
  70. valList.fill(void 0, max);
  71. next.fill(0, max);
  72. prev.fill(0, max);
  73. };
  74. return {
  75. /** Adds a key-value pair to the cache. Updates the value if the key already exists. */
  76. set(key, value) {
  77. if (key === void 0) return;
  78. let index = keyMap.get(key);
  79. if (index === void 0) {
  80. if (size === max) {
  81. index = head;
  82. const evictKey = keyList[index];
  83. onEviction == null ? void 0 : onEviction(evictKey, valList[index]);
  84. keyMap.delete(evictKey);
  85. head = next[index];
  86. prev[head] = 0;
  87. } else {
  88. index = free.length > 0 ? free.pop() : size;
  89. size++;
  90. }
  91. keyMap.set(key, index);
  92. keyList[index] = key;
  93. valList[index] = value;
  94. if (size === 1) head = tail = index;
  95. else linkTail(index);
  96. } else {
  97. onEviction == null ? void 0 : onEviction(key, valList[index]);
  98. valList[index] = value;
  99. moveToTail(index);
  100. }
  101. },
  102. /** Retrieves the value for a given key and moves the key to the most recent position. */
  103. get(key) {
  104. const index = keyMap.get(key);
  105. if (index === void 0) return;
  106. if (index !== tail) moveToTail(index);
  107. return valList[index];
  108. },
  109. /** Retrieves the value for a given key without changing its position. */
  110. peek: (key) => {
  111. const index = keyMap.get(key);
  112. return index !== void 0 ? valList[index] : void 0;
  113. },
  114. /** Checks if a key exists in the cache. */
  115. has: (key) => keyMap.has(key),
  116. /** Iterates over all keys in the cache, from most recent to least recent. */
  117. *keys() {
  118. let current = tail;
  119. for (let i = 0; i < size; i++) {
  120. yield keyList[current];
  121. current = prev[current];
  122. }
  123. },
  124. /** Iterates over all values in the cache, from most recent to least recent. */
  125. *values() {
  126. let current = tail;
  127. for (let i = 0; i < size; i++) {
  128. yield valList[current];
  129. current = prev[current];
  130. }
  131. },
  132. /** Iterates over `[key, value]` pairs in the cache, from most recent to least recent. */
  133. *entries() {
  134. let current = tail;
  135. for (let i = 0; i < size; i++) {
  136. yield [keyList[current], valList[current]];
  137. current = prev[current];
  138. }
  139. },
  140. /** Iterates over each value-key pair in the cache, from most recent to least recent. */
  141. forEach: (callback) => {
  142. let current = tail;
  143. for (let i = 0; i < size; i++) {
  144. const key = keyList[current];
  145. const value = valList[current];
  146. callback(value, key);
  147. current = prev[current];
  148. }
  149. },
  150. /** Deletes a key-value pair from the cache. */
  151. delete(key) {
  152. const index = keyMap.get(key);
  153. if (index === void 0) return false;
  154. onEviction == null ? void 0 : onEviction(key, valList[index]);
  155. keyMap.delete(key);
  156. free.push(index);
  157. keyList[index] = void 0;
  158. valList[index] = void 0;
  159. const prevIndex = prev[index];
  160. const nextIndex = next[index];
  161. if (index === head) head = nextIndex;
  162. else next[prevIndex] = nextIndex;
  163. if (index === tail) tail = prevIndex;
  164. else prev[nextIndex] = prevIndex;
  165. size--;
  166. return true;
  167. },
  168. /** Evicts the oldest item or the specified number of the oldest items from the cache. */
  169. evict: (number) => {
  170. let toPrune = Math.min(number, size);
  171. while (toPrune > 0) {
  172. const evictHead = head;
  173. const key = keyList[evictHead];
  174. onEviction == null ? void 0 : onEviction(key, valList[evictHead]);
  175. keyMap.delete(key);
  176. keyList[evictHead] = void 0;
  177. valList[evictHead] = void 0;
  178. head = next[evictHead];
  179. prev[head] = 0;
  180. size--;
  181. free.push(evictHead);
  182. toPrune--;
  183. }
  184. if (size === 0) head = tail = 0;
  185. },
  186. /** Clears all key-value pairs from the cache. */
  187. clear() {
  188. if (onEviction) {
  189. let current = head;
  190. for (let i = 0; i < size; i++) {
  191. onEviction(keyList[current], valList[current]);
  192. current = next[current];
  193. }
  194. }
  195. keyMap.clear();
  196. keyList.fill(void 0);
  197. valList.fill(void 0);
  198. free = [];
  199. size = 0;
  200. head = tail = 0;
  201. },
  202. /** Resizes the cache to a new maximum size, evicting items if necessary. */
  203. resize: (newMax) => {
  204. if (!(Number.isInteger(newMax) && newMax > 0))
  205. throw new TypeError("`max` must be a positive integer");
  206. if (newMax === max) return;
  207. if (newMax < max) _shrink(newMax);
  208. else _grow(newMax);
  209. max = newMax;
  210. },
  211. /** Returns the maximum number of items that can be stored in the cache. */
  212. get max() {
  213. return max;
  214. },
  215. /** Returns the number of items currently stored in the cache. */
  216. get size() {
  217. return size;
  218. },
  219. /** Returns the number of currently available slots in the cache before reaching the maximum size. */
  220. get available() {
  221. return max - size;
  222. }
  223. };
  224. };
  225. export {
  226. createLRU
  227. };