index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. class QuickLRU {
  3. constructor(options = {}) {
  4. if (!(options.maxSize && options.maxSize > 0)) {
  5. throw new TypeError('`maxSize` must be a number greater than 0');
  6. }
  7. if (typeof options.maxAge === 'number' && options.maxAge === 0) {
  8. throw new TypeError('`maxAge` must be a number greater than 0');
  9. }
  10. this.maxSize = options.maxSize;
  11. this.maxAge = options.maxAge || Infinity;
  12. this.onEviction = options.onEviction;
  13. this.cache = new Map();
  14. this.oldCache = new Map();
  15. this._size = 0;
  16. }
  17. _emitEvictions(cache) {
  18. if (typeof this.onEviction !== 'function') {
  19. return;
  20. }
  21. for (const [key, item] of cache) {
  22. this.onEviction(key, item.value);
  23. }
  24. }
  25. _deleteIfExpired(key, item) {
  26. if (typeof item.expiry === 'number' && item.expiry <= Date.now()) {
  27. if (typeof this.onEviction === 'function') {
  28. this.onEviction(key, item.value);
  29. }
  30. return this.delete(key);
  31. }
  32. return false;
  33. }
  34. _getOrDeleteIfExpired(key, item) {
  35. const deleted = this._deleteIfExpired(key, item);
  36. if (deleted === false) {
  37. return item.value;
  38. }
  39. }
  40. _getItemValue(key, item) {
  41. return item.expiry ? this._getOrDeleteIfExpired(key, item) : item.value;
  42. }
  43. _peek(key, cache) {
  44. const item = cache.get(key);
  45. return this._getItemValue(key, item);
  46. }
  47. _set(key, value) {
  48. this.cache.set(key, value);
  49. this._size++;
  50. if (this._size >= this.maxSize) {
  51. this._size = 0;
  52. this._emitEvictions(this.oldCache);
  53. this.oldCache = this.cache;
  54. this.cache = new Map();
  55. }
  56. }
  57. _moveToRecent(key, item) {
  58. this.oldCache.delete(key);
  59. this._set(key, item);
  60. }
  61. * _entriesAscending() {
  62. for (const item of this.oldCache) {
  63. const [key, value] = item;
  64. if (!this.cache.has(key)) {
  65. const deleted = this._deleteIfExpired(key, value);
  66. if (deleted === false) {
  67. yield item;
  68. }
  69. }
  70. }
  71. for (const item of this.cache) {
  72. const [key, value] = item;
  73. const deleted = this._deleteIfExpired(key, value);
  74. if (deleted === false) {
  75. yield item;
  76. }
  77. }
  78. }
  79. get(key) {
  80. if (this.cache.has(key)) {
  81. const item = this.cache.get(key);
  82. return this._getItemValue(key, item);
  83. }
  84. if (this.oldCache.has(key)) {
  85. const item = this.oldCache.get(key);
  86. if (this._deleteIfExpired(key, item) === false) {
  87. this._moveToRecent(key, item);
  88. return item.value;
  89. }
  90. }
  91. }
  92. set(key, value, {maxAge = this.maxAge === Infinity ? undefined : Date.now() + this.maxAge} = {}) {
  93. if (this.cache.has(key)) {
  94. this.cache.set(key, {
  95. value,
  96. maxAge
  97. });
  98. } else {
  99. this._set(key, {value, expiry: maxAge});
  100. }
  101. }
  102. has(key) {
  103. if (this.cache.has(key)) {
  104. return !this._deleteIfExpired(key, this.cache.get(key));
  105. }
  106. if (this.oldCache.has(key)) {
  107. return !this._deleteIfExpired(key, this.oldCache.get(key));
  108. }
  109. return false;
  110. }
  111. peek(key) {
  112. if (this.cache.has(key)) {
  113. return this._peek(key, this.cache);
  114. }
  115. if (this.oldCache.has(key)) {
  116. return this._peek(key, this.oldCache);
  117. }
  118. }
  119. delete(key) {
  120. const deleted = this.cache.delete(key);
  121. if (deleted) {
  122. this._size--;
  123. }
  124. return this.oldCache.delete(key) || deleted;
  125. }
  126. clear() {
  127. this.cache.clear();
  128. this.oldCache.clear();
  129. this._size = 0;
  130. }
  131. resize(newSize) {
  132. if (!(newSize && newSize > 0)) {
  133. throw new TypeError('`maxSize` must be a number greater than 0');
  134. }
  135. const items = [...this._entriesAscending()];
  136. const removeCount = items.length - newSize;
  137. if (removeCount < 0) {
  138. this.cache = new Map(items);
  139. this.oldCache = new Map();
  140. this._size = items.length;
  141. } else {
  142. if (removeCount > 0) {
  143. this._emitEvictions(items.slice(0, removeCount));
  144. }
  145. this.oldCache = new Map(items.slice(removeCount));
  146. this.cache = new Map();
  147. this._size = 0;
  148. }
  149. this.maxSize = newSize;
  150. }
  151. * keys() {
  152. for (const [key] of this) {
  153. yield key;
  154. }
  155. }
  156. * values() {
  157. for (const [, value] of this) {
  158. yield value;
  159. }
  160. }
  161. * [Symbol.iterator]() {
  162. for (const item of this.cache) {
  163. const [key, value] = item;
  164. const deleted = this._deleteIfExpired(key, value);
  165. if (deleted === false) {
  166. yield [key, value.value];
  167. }
  168. }
  169. for (const item of this.oldCache) {
  170. const [key, value] = item;
  171. if (!this.cache.has(key)) {
  172. const deleted = this._deleteIfExpired(key, value);
  173. if (deleted === false) {
  174. yield [key, value.value];
  175. }
  176. }
  177. }
  178. }
  179. * entriesDescending() {
  180. let items = [...this.cache];
  181. for (let i = items.length - 1; i >= 0; --i) {
  182. const item = items[i];
  183. const [key, value] = item;
  184. const deleted = this._deleteIfExpired(key, value);
  185. if (deleted === false) {
  186. yield [key, value.value];
  187. }
  188. }
  189. items = [...this.oldCache];
  190. for (let i = items.length - 1; i >= 0; --i) {
  191. const item = items[i];
  192. const [key, value] = item;
  193. if (!this.cache.has(key)) {
  194. const deleted = this._deleteIfExpired(key, value);
  195. if (deleted === false) {
  196. yield [key, value.value];
  197. }
  198. }
  199. }
  200. }
  201. * entriesAscending() {
  202. for (const [key, value] of this._entriesAscending()) {
  203. yield [key, value.value];
  204. }
  205. }
  206. get size() {
  207. if (!this._size) {
  208. return this.oldCache.size;
  209. }
  210. let oldCacheSize = 0;
  211. for (const key of this.oldCache.keys()) {
  212. if (!this.cache.has(key)) {
  213. oldCacheSize++;
  214. }
  215. }
  216. return Math.min(this._size + oldCacheSize, this.maxSize);
  217. }
  218. }
  219. module.exports = QuickLRU;