### Install Node LRU Cache Source: https://isaacs.github.io/node-lru-cache/benchmark Installs the 'lru-cache' package, a popular implementation of the Least Recently Used cache algorithm for Node.js. ```bash npm install lru-cache ``` -------------------------------- ### Install lru-cache with npm Source: https://isaacs.github.io/node-lru-cache/index This command installs the lru-cache package as a dependency for your project using npm. ```bash npm install lru-cache --save ``` -------------------------------- ### JavaScript Object Cache Example Source: https://isaacs.github.io/node-lru-cache/index Demonstrates a basic cache implementation using a JavaScript object, highlighting potential issues with key coercion and object representation. This example illustrates how using objects as keys can lead to unexpected behavior due to string coercion. ```JavaScript const myCache = {} const set = (k, v) => (myCache[k] = v) const get = k => myCache[k] set({}, 'please hang onto this for me') set('[object Object]', 'oopsie') ``` -------------------------------- ### Add Elements to Array Start Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Inserts new elements at the beginning of an array and returns the new length of the array. ```JavaScript array.unshift(...items) ``` -------------------------------- ### Initialize and Use LRUCache in JavaScript Source: https://isaacs.github.io/node-lru-cache/index Demonstrates how to import and initialize an LRUCache instance with various options such as max size, TTL, and custom disposal logic. It also shows basic usage like setting, getting, and clearing cache entries, including handling non-string keys. ```javascript import { LRUCache } from 'lru-cache' // or: const { LRUCache } = require('lru-cache') // or in minified form for web browsers: import { LRUCache } from 'http://unpkg.com/lru-cache@9/dist/mjs/index.min.mjs' const options = { max: 500, maxSize: 5000, sizeCalculation: (value, key) => { return 1 }, dispose: (value, key, reason) => { freeFromMemoryOrWhatever(value) }, onInsert: (value, key, reason) => { logInsertionOrWhatever(key, value) }, ttl: 1000 * 60 * 5, allowStale: false, updateAgeOnGet: false, updateAgeOnHas: false, fetchMethod: async ( key, staleValue, { options, signal, context }, ) => {} } const cache = new LRUCache(options) cache.set('key', 'value') cache.get('key') // "value" var someObject = { a: 1 } cache.set(someObject, 'a value') cache.set('[object Object]', 'a different value') assert.equal(cache.get(someObject), 'a value') assert.equal(cache.get({ a: 1 }), undefined) cache.clear() // empty the cache ``` -------------------------------- ### Add rentries, rkeys, rvalues methods Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG New methods `rentries()`, `rkeys()`, and `rvalues()` have been added. These methods provide reverse iteration over the cache's entries, keys, and values, respectively, starting from the most recently used item. ```JavaScript for (const [key, value] of cache.rentries()) { console.log(key, value); } ``` -------------------------------- ### JavaScript Array fill() Method Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray The 'fill' method changes all elements in an array to a static value, from a start index to an end index. It returns the modified array. Parameters include the value to fill with, and optional start and end indices. ```javascript fill(value: number, start?: number, end?: number): this; ``` -------------------------------- ### Get Extended Entry Info from LRU Cache Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache The info method retrieves detailed information about a cache entry, including its value, size, and TTL status. It returns undefined if the key is not found. Unlike dump, the start value is the current timestamp, and ttl is the remaining TTL (negative if expired). It always returns stale values if found, so checking for expirations is recommended. ```typescript info(key: K): undefined | Entry ``` -------------------------------- ### Node.js LRU Cache Benchmarks (string data) Source: https://isaacs.github.io/node-lru-cache/benchmark Benchmark results for LRU cache implementations using string data. This table compares 'lru-cache' (multiple versions), 'mnemonist', and 'hashlru' based on operations like set, get, update, evict, and a final score. ```javascript /* name | set | get1 | update | get2 | evict | score ---|---|---|---|---|---|--- just a null obj | 8333 | 23256 | 18519 | 23810 | 8130 | 264653 just a {} | 8065 | 23256 | 18519 | 23256 | 8000 | 260697 just a Map | 7752 | 20408 | 16393 | 20408 | 6757 | 228946 lru-cache_7.5 | 6098 | 19608 | 14925 | 20000 | 4367 | 207780 lru-cache_7.0 | 6897 | 19608 | 15385 | 19608 | 4115 | 206618 lru-cache-fast_0.2 | 3953 | 16949 | 18182 | 21277 | 2874 | 197690 lru-cache_7.14 | 7143 | 18519 | 13889 | 18182 | 4167 | 195477 lru-cache_7.8 | 7143 | 19231 | 14493 | 17857 | 3937 | 195442 lru-cache_11.0 | 6897 | 17544 | 14493 | 18519 | 4255 | 194789 lru-cache_9.1 | 6944 | 17544 | 13889 | 18868 | 3922 | 194359 lru-cache_7.6 | 6289 | 18868 | 14493 | 17544 | 4329 | 193040 lru-cache_7.13 | 6993 | 17857 | 13889 | 18182 | 4098 | 192846 lru-cache_10.0 | 6897 | 17544 | 14085 | 18182 | 4115 | 191996 lru-cache_11.2 | 6849 | 17544 | 11111 | 18182 | 4673 | 191716 lru-cache_10.2 | 6944 | 17544 | 8621 | 18519 | 4673 | 191101 lru-cache_7.12 | 7143 | 17544 | 14286 | 17857 | 4016 | 190569 lru-cache_7.9 | 7143 | 19231 | 14286 | 16949 | 3891 | 190465 mnemonist_0.39_map | 6623 | 17241 | 13514 | 17241 | 5102 | 190198 lru-cache_7.18 | 6993 | 17241 | 14706 | 17857 | 4082 | 190110 lru-cache_10.4 | 6803 | 17544 | 13699 | 17857 | 4115 | 189797 lru-cache_CURRENT | 6849 | 16949 | 13699 | 18182 | 3922 | 188764 lru-cache_11.1 | 6757 | 17544 | 8475 | 18182 | 4545 | 188256 lru-cache_9.0 | 6803 | 16393 | 14085 | 18182 | 4082 | 188190 lru-cache_8.0 | 6803 | 16949 | 9434 | 18182 | 4525 | 187422 lru-cache_10.3 | 6803 | 16667 | 9346 | 18182 | 4587 | 186798 lru-cache_7.17 | 6897 | 17241 | 14286 | 17241 | 4049 | 186253 lru-cache_7.16 | 6944 | 17241 | 12195 | 17241 | 4132 | 184671 lru-cache_7.10 | 7194 | 17544 | 11236 | 16949 | 4219 | 184096 lru-cache_7.4 | 6024 | 15385 | 12048 | 15625 | 4545 | 171101 lru-cache_7.1 | 6024 | 15385 | 12195 | 15385 | 4566 | 170153 lru-cache_7.2 | 5714 | 15152 | 12346 | 15385 | 4525 | 168780 lru-cache_7.3 | 6098 | 14925 | 11905 | 15385 | 4405 | 167826 lru-cache_10.1 | 6098 | 14925 | 11765 | 15625 | 4000 | 166861 lru-cache_7.15 | 6369 | 14925 | 11494 | 14706 | 4255 | 163812 lru-cache_7.11 | 6369 | 14925 | 11364 | 14286 | 4255 | 161582 lru-cache_7.7 | 6061 | 14925 | 11765 | 14085 | 4167 | 159922 hashlru_2 | 9615 | 8929 | 8333 | 8929 | 9524 | 146615 mnemonist_0.39_obj | 8065 | 6803 | 6173 | 6897 | 4202 | 98207 */ ``` -------------------------------- ### Load Cache Entries in Node LRU Cache Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache Resets the cache and loads entries in the specified order. The resulting cache structure might differ if cache options are not consistent. Assumes 'start' fields are relative to a portable Date.now() timestamp. ```TypeScript load(arr: [K, Entry][]): void ``` -------------------------------- ### Node.js LRU Cache Benchmarks (Random Float Cache) Source: https://isaacs.github.io/node-lru-cache/benchmark This section presents performance benchmarks for LRU cache implementations using random floating-point numbers. It compares operations like set, get, update, and evict for various 'lru-cache' versions against 'mnemonist' and 'just a Map'. ```javascript name | set | get1 | update | get2 | evict | score ---|---|---|---|---|---|--- lru-cache_7.5 | 5917 | 15385 | 9804 | 16393 | 4274 | 171128 lru-cache_7.2 | 5181 | 15625 | 9174 | 16393 | 4405 | 170401 just a Map | 6061 | 14706 | 11236 | 14493 | 5618 | 168031 lru-cache_7.4 | 5848 | 15152 | 9346 | 15385 | 4237 | 164608 lru-cache_7.3 | 5291 | 15152 | 8065 | 15385 | 4292 | 162488 mnemonist_0.39_map | 6757 | 14286 | 10417 | 13514 | 4717 | 157944 lru-cache_7.8 | 6098 | 15152 | 9091 | 14085 | 4115 | 157743 lru-cache_7.7 | 6024 | 14925 | 11364 | 14085 | 3650 | 156862 lru-cache_7.10 | 6757 | 14925 | 9009 | 13889 | 4016 | 156823 lru-cache_7.1 | 5181 | 15385 | 7874 | 13889 | 4444 | 156056 lru-cache_7.6 | 5319 | 14925 | 8264 | 13889 | 4292 | 154582 lru-cache_7.9 | 6098 | 14706 | 11111 | 13514 | 3788 | 153935 lru-cache_10.1 | 5952 | 11236 | 8850 | 15625 | 4049 | 152832 lru-cache_10.2 | 5682 | 11364 | 9009 | 15625 | 3984 | 152510 lru-cache_7.0 | 4975 | 14493 | 9091 | 13699 | 4219 | 152110 lru-cache_11.0 | 5917 | 11628 | 8000 | 15385 | 4049 | 151888 lru-cache_10.4 | 5917 | 10989 | 7519 | 15625 | 4065 | 150770 lru-cache_10.3 | 5952 | 11236 | 7246 | 15385 | 4049 | 150028 lru-cache_8.0 | 5952 | 10309 | 8696 | 15385 | 4016 | 148532 lru-cache_9.1 | 5952 | 9524 | 8929 | 15625 | 4032 | 147690 lru-cache_10.0 | 5747 | 9009 | 9009 | 15152 | 3968 | 143130 lru-cache_9.0 | 5236 | 9901 | 5814 | 15385 | 3953 | 142679 lru-cache_7.14 | 6061 | 9804 | 8929 | 14286 | 3891 | 141348 lru-cache_7.15 | 5952 | 11111 | 8475 | 13514 | 3953 | 141047 lru-cache_7.12 | 6024 | 9174 | 8850 | 14286 | 3968 | 139690 lru-cache_11.1 | 5025 | 9709 | 6098 | 15152 | 3650 | 139285 lru-cache_7.18 | 5291 | 9804 | 6211 | 14286 | 3984 | 137555 lru-cache_7.11 | 4525 | 9804 | 7813 | 14286 | 3968 | 137545 lru-cache_11.2 | 4878 | 9259 | 5714 | 14706 | 3497 | 134262 lru-cache_7.17 | 5236 | 7519 | 8333 | 14286 | 3984 | 132712 lru-cache_CURRENT | 5650 | 9259 | 6410 | 13158 | 3906 | 130807 lru-cache_7.13 | 5952 | 8000 | 8929 | 12987 | 4016 | 129848 lru-cache_7.16 | 4831 | 8850 | 7353 | 12500 | 3636 | 124245 just a null obj | 2262 | 5263 | 3521 | 5208 | 2020 | 59974 just a {} | 1916 | 5076 | 3690 | 5236 | 1783 | 57845 lru-fast_0.2 | 1541 | 4566 | 4405 | 4854 | 867 | 49790 mnemonist_0.39_obj | 1898 | 3067 | 2703 | 3205 | 970 | 36575 hashlru_2 | 2045 | 2331 | 2469 | 2463 | 1946 | 35597 ``` -------------------------------- ### Node.js LRU Cache Benchmarks (numeric string data) Source: https://isaacs.github.io/node-lru-cache/benchmark Benchmark results for LRU cache implementations using numeric string data. This table compares 'lru-cache' (multiple versions), 'mnemonist', and 'hashlru' based on operations like set, get, update, evict, and a final score. ```javascript /* name | set | get1 | update | get2 | evict | score ---|---|---|---|---|---|--- just a Map | 6098 | 21277 | 17241 | 21277 | 7692 | 238113 lru-cache_11.0 | 7194 | 17544 | 14925 | 20408 | 5587 | 211920 lru-cache_7.5 | 6135 | 17544 | 14925 | 21277 | 5000 | 211212 lru-cache_7.2 | 6135 | 17241 | 13158 | 21739 | 5000 | 210846 lru-cache_7.0 | 6173 | 16949 | 13158 | 21739 | 5025 | 210171 lru-cache_7.1 | 6024 | 17544 | 12987 | 20833 | 5000 | 206832 mnemonist_0.39_map | 6993 | 18182 | 14706 | 18182 | 5848 | 203388 lru-cache_11.2 | 6944 | 15385 | 9091 | 20408 | 5236 | 197354 lru-cache_7.14 | 5587 | 15873 | 12346 | 18868 | 5405 | 192504 lru-cache_7.9 | 6098 | 15873 | 12195 | 18182 | 4673 | 186285 lru-cache_7.4 | 6494 | 16393 | 11494 | 16667 | 5263 | 183311 lru-cache_11.1 | 5263 | 16667 | 11765 | 17241 | 4608 | 181537 lru-cache_7.3 | 6061 | 16393 | 12195 | 15873 | 5208 | 178901 lru-cache_7.6 | 6667 | 16129 | 12195 | 15625 | 5319 | 178636 lru-cache_8.0 | 6579 | 15873 | 12195 | 16129 | 4950 | 178367 lru-cache_9.0 | 6536 | 15873 | 11494 | 16129 | 5025 | 177955 lru-cache_9.1 | 6452 | 16129 | 11905 | 15873 | 4975 | 177436 lru-cache_10.0 | 6369 | 16129 | 11364 | 15873 | 4975 | 176729 lru-cache_10.1 | 6250 | 15625 | 10638 | 16393 | 4950 | 176728 lru-cache_CURRENT | 6289 | 16129 | 11628 | 15873 | 4878 | 176348 lru-cache_7.7 | 6579 | 16129 | 12346 | 15625 | 4854 | 176286 lru-cache_10.2 | 6289 | 16129 | 10870 | 15873 | 5000 | 176200 lru-cache_7.13 | 6579 | 16667 | 11364 | 15385 | 4926 | 176078 lru-cache_7.12 | 6135 | 16393 | 12346 | 15385 | 4878 | 175110 lru-cache_10.3 | 5952 | 15873 | 11628 | 16129 | 4608 | 174836 lru-cache_7.11 | 6250 | 16393 | 12048 | 15385 | 4785 | 174577 lru-cache_7.18 | 6024 | 16129 | 12500 | 15625 | 4587 | 173995 lru-cache_7.17 | 6289 | 15625 | 12195 | 15625 | 4831 | 173928 lru-cache_7.16 | 6289 | 16129 | 12048 | 15385 | 4762 | 173748 lru-cache_7.8 | 6173 | 15625 | 11628 | 15625 | 4785 | 172899 lru-cache_7.15 | 6250 | 15873 | 12048 | 15385 | 4739 | 172787 lru-cache_10.4 | 5714 | 15873 | 11111 | 16129 | 4310 | 172353 lru-cache_7.10 | 6098 | 15625 | 11628 | 15152 | 4926 | 171089 mnemonist_0.39_obj | 0 | 0 | 0 | 0 | 0 | 0 hashlru_2 | 0 | 0 | 0 | 0 | 0 | 0 */ ``` -------------------------------- ### Benchmark Node.js LRU Cache Performance Source: https://isaacs.github.io/node-lru-cache/benchmark This snippet represents the performance data collected for various Node.js LRU cache implementations. It includes metrics for 'set', 'get', 'update', 'evict', and 'score' operations across different cache versions and data types like null objects, empty objects, Maps, and long strings. ```javascript /* Project: /websites/isaacs_github_io_node-lru-cache Data represents performance benchmarks for different LRU cache implementations. Columns: name, set, get1, update, get2, evict, score Example data points: sym: a Symbol object name | set | get1 | update | get2 | evict | score ---|---|---|---|---|---|--- just a null obj | 8475 | 24390 | 18519 | 24390 | 8403 | 272604 just a {} | 8547 | 23810 | 18868 | 24390 | 8065 | 269667 just a Map | 8197 | 20833 | 17241 | 21277 | 7692 | 240979 lru-cache_7.2 | 7576 | 21739 | 16393 | 22727 | 5051 | 235652 ... longstr: a very long string name | set | get1 | update | get2 | evict | score ---|---|---|---|---|---|--- just a null obj | 9259 | 27778 | 10638 | 27778 | 7937 | 291065 just a {} | 7813 | 21277 | 8547 | 21277 | 6711 | 227944 just a Map | 6711 | 17857 | 7692 | 17241 | 6211 | 191945 lru-cache_7.5 | 5618 | 17857 | 12195 | 18182 | 3953 | 187677 ... hashlru_2 | 8621 | 8403 | 8547 | 8475 | 8547 | 136108 mnemonist_0.39_obj | 5236 | 5952 | 5208 | 6250 | 4255 | 86061 */ // This is a representation of benchmark data, not executable code. // The actual implementation would involve setting up and running tests // using a benchmarking library like 'benchmark' or 'autocannon'. // Example of how one might structure the data for analysis: const benchmarkData = { "sym": { "just a null obj": { "set": 8475, "get1": 24390, "update": 18519, "get2": 24390, "evict": 8403, "score": 272604 }, "just a {}": { "set": 8547, "get1": 23810, "update": 18868, "get2": 24390, "evict": 8065, "score": 269667 }, "just a Map": { "set": 8197, "get1": 20833, "update": 17241, "get2": 21277, "evict": 7692, "score": 240979 }, "lru-cache_7.2": { "set": 7576, "get1": 21739, "update": 16393, "get2": 22727, "evict": 5051, "score": 235652 } // ... more data }, "longstr": { "just a null obj": { "set": 9259, "get1": 27778, "update": 10638, "get2": 27778, "evict": 7937, "score": 291065 }, "just a {}": { "set": 7813, "get1": 21277, "update": 8547, "get2": 21277, "evict": 6711, "score": 227944 }, "just a Map": { "set": 6711, "get1": 17857, "update": 7692, "get2": 17241, "evict": 6211, "score": 191945 }, "lru-cache_7.5": { "set": 5618, "get1": 17857, "update": 12195, "get2": 18182, "evict": 3953, "score": 187677 } // ... more data } }; console.log("Benchmark data loaded."); ``` -------------------------------- ### Add status option for internal state investigation Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG Introduces a `status` option for methods like `has()`, `set()`, `get()`, and `fetch()`. This allows for internal state investigation, providing insights into why an item might be in a particular state (e.g., stale, expired). ```JavaScript const status = cache.has('myKey', { status: true }); console.log(status); ``` -------------------------------- ### Node.js LRU Cache Benchmarks (Object Cache) Source: https://isaacs.github.io/node-lru-cache/benchmark This section details the performance of various LRU cache implementations when used with an object as the underlying data structure. It compares operations like set, get, update, and evict across different versions of 'lru-cache' and other libraries such as 'mnemonist' and 'hashlru'. ```javascript name | set | get1 | update | get2 | evict | score ---|---|---|---|---|---|--- just a Map | 8333 | 21739 | 18182 | 22222 | 8065 | 251500 lru-cache_7.5 | 7874 | 22222 | 16129 | 22222 | 5263 | 235968 lru-cache_7.7 | 8130 | 21277 | 12500 | 20000 | 5495 | 220066 lru-cache_11.1 | 5682 | 19608 | 14706 | 21277 | 5525 | 218904 lru-cache_7.6 | 8000 | 21277 | 12048 | 19231 | 5848 | 217274 lru-cache_10.1 | 5556 | 18868 | 14085 | 21277 | 5405 | 215211 lru-cache_9.0 | 5618 | 19608 | 14706 | 20408 | 5464 | 214126 lru-cache_7.16 | 6536 | 16667 | 15385 | 20833 | 5556 | 210403 lru-cache_7.9 | 8000 | 20833 | 11765 | 18182 | 5405 | 208199 lru-cache_9.1 | 6135 | 16393 | 14286 | 20833 | 5464 | 207220 lru-cache_7.12 | 6452 | 15625 | 15385 | 20000 | 5587 | 203099 mnemonist_0.39_map | 7299 | 18519 | 14925 | 17544 | 5848 | 202040 lru-cache_7.3 | 6897 | 17241 | 12821 | 17544 | 5464 | 193378 lru-cache_7.4 | 6849 | 17544 | 12821 | 17241 | 5263 | 191671 lru-cache_7.1 | 6897 | 17241 | 12658 | 17241 | 5435 | 191555 lru-cache_7.0 | 6711 | 17544 | 12658 | 16949 | 5376 | 190337 lru-cache_7.2 | 6757 | 17241 | 12821 | 16949 | 5348 | 189543 lru-cache_11.0 | 6849 | 16949 | 11905 | 16393 | 5025 | 183540 lru-cache_CURRENT | 6897 | 16667 | 12195 | 16393 | 5102 | 183465 lru-cache_11.2 | 6173 | 16393 | 11628 | 16667 | 4878 | 180878 lru-cache_7.10 | 6993 | 16393 | 12500 | 15873 | 5076 | 180410 lru-cache_7.18 | 6757 | 16667 | 12500 | 15873 | 4926 | 180010 lru-cache_10.0 | 6803 | 16129 | 11628 | 16393 | 4878 | 179976 lru-cache_7.17 | 6579 | 16949 | 12346 | 15625 | 5076 | 179856 lru-cache_10.3 | 6250 | 16129 | 11765 | 16129 | 4975 | 178172 lru-cache_7.11 | 6536 | 16667 | 12346 | 15385 | 5025 | 177469 lru-cache_7.15 | 6494 | 16393 | 11765 | 15625 | 5076 | 177437 lru-cache_10.4 | 6579 | 15625 | 11494 | 16129 | 4975 | 177047 lru-cache_10.2 | 5882 | 16129 | 11905 | 16393 | 4545 | 176746 lru-cache_8.0 | 5747 | 16667 | 11765 | 16393 | 4292 | 176685 lru-cache_7.14 | 6452 | 16393 | 12048 | 15385 | 5000 | 176056 lru-cache_7.8 | 6250 | 15873 | 12500 | 15625 | 4878 | 175134 lru-cache_7.13 | 5208 | 9804 | 8929 | 12500 | 3559 | 129052 mnemonist_0.39_obj | 0 | 0 | 0 | 0 | 0 | 0 hashlru_2 | 0 | 0 | 0 | 0 | 0 | 0 lru-fast_0.2 | 0 | 0 | 0 | 0 | 0 | 0 just a null obj | 0 | 0 | 0 | 0 | 0 | 0 just a {} | 0 | 0 | 0 | 0 | 0 | 0 ``` -------------------------------- ### Get Cache Keys in Reverse Order (Node LRU Cache) Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache Returns a generator that yields cache keys in order from least recently used to most recently used. This is the inverse of the standard keys() method. ```TypeScript rkeys(): Generator ``` -------------------------------- ### Implement Map interface in Node LRU Cache Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG In version 10.2, the LRUCache implements the standard `Map` interface. This means it now supports methods like `get()`, `set()`, `has()`, `delete()`, `clear()`, and iteration protocols, making it more interoperable with other JavaScript data structures and APIs. ```TypeScript const cache: LRUCache = new LRUCache(); cache.set('a', 1); console.log(cache.get('a')); // 1 ``` -------------------------------- ### Basic LRU Cache Usage (JavaScript) Source: https://isaacs.github.io/node-lru-cache/benchmark Demonstrates the basic usage of the 'lru-cache' package in JavaScript. It shows how to create a cache with a maximum size and add/retrieve items. ```javascript const LRU = require('lru-cache'); // Create a cache with a max size of 100 and max age of 5 minutes const cache = new LRU({ max: 100, maxAge: 1000 * 60 * 5 // 5 minutes in milliseconds }); // Set a value cache.set('mykey', 'myvalue'); // Get a value const value = cache.get('mykey'); console.log(value); // Output: myvalue // Check if a key exists console.log(cache.has('mykey')); // Output: true // Delete a key cache.delete('mykey'); console.log(cache.has('mykey')); // Output: false ``` -------------------------------- ### Run Node LRU Cache Benchmark Source: https://isaacs.github.io/node-lru-cache/benchmark This command executes the benchmark script for Node.js LRU caches. It sets the NODE_ENV to 'production' for optimized performance and runs the benchmark using Node.js. ```bash NODE_ENV=production node index.js ``` -------------------------------- ### Instantiate LRUCache Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache Creates a new instance of the LRUCache class. The constructor accepts an options object which can be either an LRUCache instance or a specific Options object. Type parameters K, V, and FC define the key, value, and fetch context types respectively. ```typescript new LRUCache(options: LRUCache | Options) ``` -------------------------------- ### Get Array Iterator Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Returns an iterable of values in the array. ```JavaScript array.values() ``` -------------------------------- ### Array.prototype.indexOf() - TypeScript Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Returns the first index at which a given element can be found in the array, or -1 if it is not present. The search can start from an optional index. ```TypeScript indexOf(searchElement: number, fromIndex?: number): number ``` -------------------------------- ### Array.prototype.includes() - TypeScript Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Checks if an array contains a specific element, returning true or false. The search can optionally start from a specified index. ```TypeScript includes(searchElement: number, fromIndex?: number): boolean ``` -------------------------------- ### Improve AbortController polyfill Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG The AbortController polyfill has been enhanced to better support `signal.addEventListener('abort')` and `signal.onabort`. Additionally, a fix ensures that items are dropped from the cache instead of causing unhandled rejections when the `fetchMethod` throws an error or returns a rejected Promise. ```JavaScript const controller = new AbortController(); const signal = controller.signal; signal.addEventListener('abort', () => { console.log('Aborted!'); }); controller.abort(); ``` -------------------------------- ### Array.prototype.lastIndexOf() - TypeScript Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Returns the last index at which a given element can be found in the array, or -1 if it is not present. The search can start backward from an optional index. ```TypeScript lastIndexOf(searchElement: number, fromIndex?: number): number ``` -------------------------------- ### Extract Array Section Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray The slice method creates a shallow copy of a portion of an array. It accepts optional start and end indices, where negative indices count from the end of the array. ```javascript slice(start?: number, end?: number): number[] ``` -------------------------------- ### TypeScript: Array Length Property Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Describes the 'length' property of an array in TypeScript, which gets or sets the number of elements in the array. It's typically one higher than the highest index. ```typescript length: number ``` -------------------------------- ### Stack Interface Definition Source: https://isaacs.github.io/node-lru-cache/interfaces/Stack Defines the structure of a Stack, including its properties 'heap' (a NumberArray) and 'length' (a number), along with the 'pop' and 'push' methods. The 'pop' method returns an Index, and the 'push' method accepts an Index and returns void. ```TypeScript interface Stack { heap: NumberArray; length: number; pop(): Index; push(n: Index): void; } ``` -------------------------------- ### Splice Array - Remove and Insert Elements Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Removes elements from an array starting at a specified index and inserts new elements in their place. The method returns an array containing the deleted elements. ```JavaScript array.splice(start, deleteCount, ...items) ``` -------------------------------- ### Splice Array - Remove Elements Source: https://isaacs.github.io/node-lru-cache/interfaces/ZeroArray Removes elements from an array starting at a specified index. Optionally, a number of elements to remove can be specified. The method returns an array containing the deleted elements. ```JavaScript array.splice(start, deleteCount) ``` -------------------------------- ### Get Cache Values in Reverse Order (Node LRU Cache) Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache Returns a generator that yields cache values in order from least recently used to most recently used. This is the inverse of the standard values() method. ```TypeScript rvalues(): Generator ``` -------------------------------- ### LRU Cache with Custom Max Age (JavaScript) Source: https://isaacs.github.io/node-lru-cache/benchmark Illustrates creating an LRU cache where items have a custom maximum age. This is useful for time-sensitive data. ```javascript const LRU = require('lru-cache'); const cache = new LRU({ max: 50, maxAge: 1000 * 60 * 10 // 10 minutes }); cache.set('shortlived', 'will expire soon', 1000 * 60 * 1); // Expires in 1 minute cache.set('longlived', 'will expire in 10 minutes'); // Expires in 10 minutes setTimeout(() => { console.log('After 2 minutes:'); console.log(cache.get('shortlived')); // Output: undefined console.log(cache.get('longlived')); // Output: will expire in 10 minutes }, 1000 * 60 * 2); ``` -------------------------------- ### Add cache.info(key) to Node LRU Cache Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG The `cache.info(key)` method has been introduced to retrieve detailed information about a cached item, including its value, Time-To-Live (TTL), and size. This provides greater visibility into the cache's state for specific keys. ```JavaScript const info = cache.info('myKey'); console.log(info.value, info.ttl, info.size); ``` -------------------------------- ### Add noDeleteOnStaleGet option Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG The `noDeleteOnStaleGet` option prevents the cache from removing a stale item when `get()` is called on it. By default, accessing a stale item might remove it; this option preserves the stale item in the cache. ```JavaScript const cache = new LRUCache({ noDeleteOnStaleGet: true }); ``` -------------------------------- ### Retrieve Value from LRU Cache Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache The get method retrieves a value from the cache using its key. Accessing an item updates its recency. If the key is not found in the cache, it returns undefined. Optional getOptions can be provided for specific retrieval behaviors. ```typescript get(k: K, getOptions?: GetOptions): undefined | V ``` -------------------------------- ### LRU Cache with Size Calculation (JavaScript) Source: https://isaacs.github.io/node-lru-cache/benchmark Shows how to configure an LRU cache that uses a custom size calculation function. This allows for more complex size management beyond simple item counts. ```javascript const LRU = require('lru-cache'); // Cache items based on their string length const cache = new LRU({ max: 1000, sizeCalculation: (value, key) => { return value.toString().length; } }); cache.set('key1', 'a'); // size: 1 cache.set('key2', 'bb'); // size: 2 cache.set('key3', 'ccc'); // size: 3 console.log(cache.size); // Output: 6 (1 + 2 + 3) ``` -------------------------------- ### Add reason to dispose() calls Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG Dispose calls now include a reason parameter. This allows the `dispose` callback (if provided) to understand why an item was removed from the cache, such as expiration, eviction due to size, or explicit deletion. ```JavaScript cache.set('item', value, { dispose: (val, reason) => { console.log(`Disposed item because: ${reason}`); } }); ``` -------------------------------- ### Add disposeAfter() method Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG The `disposeAfter()` method has been added, allowing you to specify a callback function that will be executed after an item is disposed from the cache. This can be used for cleanup operations or logging. ```JavaScript cache.set('resource', data, { disposeAfter: (value) => { console.log('Disposing:', value); } }); ``` -------------------------------- ### SetOptions Interface Definition Source: https://isaacs.github.io/node-lru-cache/interfaces/LRUCache Defines the structure for options that can be passed to the LRUCache#set method. These options control aspects like entry disposal, TTL updates, size tracking, and time-to-live. ```TypeScript interface SetOptions { noDisposeOnSet?: boolean; noUpdateTTL?: boolean; size?: number; sizeCalculation?: SizeCalculator; start?: number; status?: Status; ttl?: number; } ``` -------------------------------- ### Get Remaining TTL for LRU Cache Entry Source: https://isaacs.github.io/node-lru-cache/classes/LRUCache The getRemainingTTL method returns the remaining time-to-live (TTL) in milliseconds for a given cache key. If the key is not in the cache, it returns 0. For items present in the cache without a defined TTL, it returns Infinity. ```typescript getRemainingTTL(key: K): number ``` -------------------------------- ### LRU Cache with Dispose Function (JavaScript) Source: https://isaacs.github.io/node-lru-cache/benchmark Demonstrates using a 'dispose' function with the LRU cache. This function is called when an item is evicted from the cache, allowing for cleanup of resources. ```javascript const LRU = require('lru-cache'); const cache = new LRU({ max: 3, dispose: (value, key) => { console.log(`Disposing key: ${key}, value: ${value}`); } }); cache.set('a', 'apple'); cache.set('b', 'banana'); cache.set('c', 'cherry'); cache.set('d', 'date'); // 'a' should be evicted // Expected output: Disposing key: a, value: apple ``` -------------------------------- ### Add fetchContext option to Node LRU Cache Source: https://isaacs.github.io/node-lru-cache/media/CHANGELOG The `fetchContext` option provides a way to pass additional information to the `fetchMethod`. This context can be used by the fetch logic to make more informed decisions, such as passing request-specific headers or user data. ```JavaScript const cache = new LRUCache({ fetchMethod: async (key, context) => { console.log('Context:', context); // ... fetch logic } }); cache.fetch('data', { context: { userId: 123 } }); ```