{"name":"lru-cache","description":"A cache object that deletes the least-recently-used items.","version":"7.0.4","publishConfig":{"tag":"v7.0-backport"},"author":{"name":"Isaac Z. Schlueter","email":"i@izs.me"},"keywords":["mru","lru","cache"],"scripts":{"test":"tap","snap":"tap","preversion":"npm test","postversion":"npm publish","prepublishOnly":"git push origin --follow-tags"},"main":"index.js","repository":{"type":"git","url":"git://github.com/isaacs/node-lru-cache.git"},"devDependencies":{"benchmark":"^2.1.4","heapdump":"^0.3.15","tap":"^16.0.1"},"license":"ISC","engines":{"node":">=12"},"tap":{"coverage-map":"map.js","node-arg":["--expose-gc"]},"readme":"# lru-cache\n\nA cache object that deletes the least-recently-used items.\n\nSpecify a max number of the most recently used items that you want to keep,\nand this cache will keep that many of the most recently accessed items.\n\nThis is not primarily a TTL cache, and does not make strong TTL guarantees.\nThere is no preemptive pruning of expired items, but you _may_ set a TTL\non the cache or on a single `set`.  If you do so, it will treat expired\nitems as missing, and delete them when fetched.\n\nAs of version 7, this is one of the most performant LRU implementations\navailable in JavaScript, and supports a wide diversity of use cases.\nHowever, note that using some of the features will necessarily impact\nperformance, by causing the cache to have to do more work.  See the\n\"Performance\" section below.\n\n## Installation\n\n```js\nnpm install lru-cache --save\n```\n\n## Usage\n\n```js\nconst LRU = require('lru-cache')\n\n// only 'max' is required, the others are optional, but MAY be\n// required if certain other fields are set.\nconst options = {\n  // the number of most recently used items to keep.\n  // note that we may store fewer items than this if maxSize is hit.\n  max: 500,\n\n  // if you wish to track item size, you must provide a maxSize\n  // note that we still will only keep up to max *actual items*,\n  // so size tracking may cause fewer than max items to be stored.\n  // At the extreme, a single item of maxSize size will cause everything\n  // else in the cache to be dropped when it is added.  Use with caution!\n  // Note also that size tracking can negatively impact performance,\n  // though for most cases, only minimally.\n  maxSize: 5000,\n\n  // function to calculate size of items.  useful if storing strings or\n  // buffers or other items where memory size depends on the object itself.\n  // also note that oversized items do NOT immediately get dropped from\n  // the cache, though they will cause faster turnover in the storage.\n  sizeCalculation: (value, key) => {\n    // return an positive integer which is the size of the item,\n    // if a positive integer is not returned, will use 0 as the size.\n    return 1\n  },\n\n  // function to call when the item is removed from the cache\n  // Note that using this can negatively impact performance.\n  dispose: (value, key) => {\n    freeFromMemoryOrWhatever(value)\n  },\n\n  // max time to live for items before they are considered stale\n  // note that stale items are NOT preemptively removed, and MAY\n  // live in the cache, contributing to its LRU max, long after they\n  // have expired.\n  // Also, as this cache is optimized for LRU/MRU operations, some of\n  // the staleness/TTL checks will reduce performance, as they will incur\n  // overhead by deleting items.\n  // Must be a positive integer in ms, defaults to 0, which means \"no TTL\"\n  ttl: 1000 * 60 * 5,\n\n  // return stale items from cache.get() before disposing of them\n  // boolean, default false\n  allowStale: false,\n\n  // update the age of items on cache.get(), renewing their TTL\n  // boolean, default false\n  updateAgeOnGet: false,\n\n  // update the age of items on cache.has(), renewing their TTL\n  // boolean, default false\n  updateAgeOnHas: false,\n\n  // update the \"recently-used\"-ness of items on cache.has()\n  // boolean, default false\n  updateRecencyOnHas: false,\n}\n\nconst cache = new LRU(options)\n\ncache.set(\"key\", \"value\")\ncache.get(\"key\") // \"value\"\n\n// non-string keys ARE fully supported\n// but note that it must be THE SAME object, not\n// just a JSON-equivalent object.\nvar someObject = { a: 1 }\ncache.set(someObject, 'a value')\n// Object keys are not toString()-ed\ncache.set('[object Object]', 'a different value')\nassert.equal(cache.get(someObject), 'a value')\n// A similar object with same keys/values won't work,\n// because it's a different object identity\nassert.equal(cache.get({ a: 1 }), undefined)\n\ncache.clear()    // empty the cache\n```\n\nIf you put more stuff in it, then items will fall out.\n\n## Options\n\n* `max` - The maximum number (or size) of items that remain in the cache\n  (assuming no TTL pruning or explicit deletions).  Note that fewer items\n  may be stored if size calculation is used, and `maxSize` is exceeded.\n  This must be a positive finite intger.\n\n* `maxSize` - Set to a positive integer to track the sizes of items added\n  to the cache, and automatically evict items in order to stay below this\n  size.  Note that this may result in fewer than `max` items being stored.\n\n* `sizeCalculation` - Function used to calculate the size of stored\n  items.  If you're storing strings or buffers, then you probably want to\n  do something like `n => n.length`.  The item is passed as the first\n  argument, and the key is passed as the second argumnet.\n\n    This may be overridden by passing an options object to `cache.set()`.\n\n    Requires `maxSize` to be set.\n\n    Deprecated alias: `length`\n\n* `dispose` Function that is called on items when they are dropped\n  from the cache.  This can be handy if you want to close file\n  descriptors or do other cleanup tasks when items are no longer\n  stored in the cache.\n\n    It is called *after* the item has been fully removed from the cache, so\n    if you want to put it right back in, that is safe to do.\n\n    Unlike several other options, this may _not_ be overridden by passing\n    an option to `set()`, for performance reasons.  If disposal functions\n    may vary between cache entries, then the entire list must be scanned\n    on every cache swap, even if no disposal function is in use.\n\n    Optional, must be a function.\n\n* `noDisposeOnSet` Set to `true` to suppress calling the `dispose()`\n  function if the entry key is still accessible within the cache.\n\n    This may be overridden by passing an options object to `cache.set()`.\n\n    Boolean, default `false`.  Only relevant if `dispose` option is set.\n\n* `ttl` - max time to live for items before they are considered stale.\n  Note that stale items are NOT preemptively removed, and MAY live in the\n  cache, contributing to its LRU max, long after they have expired.\n\n    Also, as this cache is optimized for LRU/MRU operations, some of\n    the staleness/TTL checks will reduce performance, as they will incur\n    overhead by deleting from Map objects rather than simply throwing old\n    Map objects away.\n\n    This is not primarily a TTL cache, and does not make strong TTL\n    guarantees.  There is no pre-emptive pruning of expired items, but you\n    _may_ set a TTL on the cache, and it will treat expired items as missing\n    when they are fetched, and delete them.\n\n    Optional, but must be a positive integer in ms if specified.\n\n    This may be overridden by passing an options object to `cache.set()`.\n\n    Deprecated alias: `maxAge`\n\n* `allowStale` - By default, if you set `ttl`, it'll only delete stale\n  items from the cache when you `get(key)`.  That is, it's not\n  pre-emptively pruning items.\n\n    If you set `allowStale:true`, it'll return the stale value as well as\n    deleting it.  If you don't set this, then it'll return `undefined` when\n    you try to get a stale entry.\n\n    Note that when a stale entry is fetched, _even if it is returned due to\n    `allowStale` being set_, it is removed from the cache immediately.  You\n    can immediately put it back in the cache if you wish, thus resetting the\n    TTL.\n\n    This may be overridden by passing an options object to `cache.get()`.\n    The `cache.has()` method will always return `false` for stale items.\n\n    Boolean, default false, only relevant if `ttl` is set.\n\n    Deprecated alias: `stale`\n\n* `updateAgeOnGet` - When using time-expiring entries with `ttl`, setting\n  this to `true` will make each item's age reset to 0 whenever it is\n  retrieved from cache with `get()`, causing it to not expire.  (It can\n  still fall out of cache based on recency of use, of course.)\n\n    This may be overridden by passing an options object to `cache.get()`.\n\n    Boolean, default false, only relevant if `ttl` is set.\n\n## API\n\n* `new LRUCache(options)`\n\n    Create a new LRUCache.  All options are documented above, and are on\n    the cache as public members.\n\n* `cache.max`, `cache.ttl`, `cache.allowStale`, etc.\n\n    All option names are exposed as public members on the cache object.\n\n    These are intended for read access only.  Changing them during program\n    operation can cause undefined behavior.\n\n* `cache.size`\n\n    The total number of items held in the cache at the current moment.\n\n* `cache.calculatedSize`\n\n    The total size of items in cache when using size tracking.\n\n* `set(key, value, [{ size, sizeCalculation, ttl, noDisposeOnSet }])`\n\n    Add a value to the cache.\n\n    Optional options object may contain `ttl` and `sizeCalculation` as\n    described above, which default to the settings on the cache object.\n\n    Options object my also include `size`, which will prevent calling the\n    `sizeCalculation` function and just use the specified number if it is a\n    positive integer, and `noDisposeOnSet` which will prevent calling a\n    `dispose` function in the case of overwrites.\n\n    Will update the recency of the entry.\n\n* `get(key, { updateAgeOnGet, allowStale } = {}) => value`\n\n    Return a value from the cache.\n\n    Will update the recency of the cache entry found.\n\n    If the key is not found, `get()` will return `undefined`.  This can be\n    confusing when setting values specifically to `undefined`, as in\n    `cache.set(key, undefined)`.  Use `cache.has()` to determine whether a\n    key is present in the cache at all.\n\n* `peek(key, { allowStale } = {}) => value`\n\n    Like `get()` but doesn't update recency or delete stale items.\n\n    Returns `undefined` if the item is stale, unless `allowStale` is set\n    either on the cache or in the options object.\n\n* `has(key)`\n\n    Check if a key is in the cache, without updating the recency or age.\n\n    Will return `false` if the item is stale, even though it is technically\n    in the cache.\n\n* `delete(key)`\n\n    Deletes a key out of the cache.\n\n* `clear()`\n\n    Clear the cache entirely, throwing away all values.\n\n    Deprecated alias: `reset()`\n\n* `keys()`\n\n    Return a generator yielding the keys in the cache.\n\n* `values()`\n\n    Return a generator yielding the values in the cache.\n\n* `entries()`\n\n    Return a generator yielding `[key, value]` pairs.\n\n* `find(fn, [getOptions])`\n\n    Find a value for which the supplied `fn` method returns a truthy value,\n    similar to `Array.find()`.\n\n    `fn` is called as `fn(value, key, cache)`.\n\n    The optional `getOptions` are applied to the resulting `get()` of the\n    item found.\n\n* `dump()`\n\n    Return an array of `[key, entry]` objects which can be passed to\n    `cache.load()`\n\n    Note: this returns an actual array, not a generator, so it can be more\n    easily passed around.\n\n* `load(entries)`\n\n    Reset the cache and load in the items in `entries` in the order listed.\n    Note that the shape of the resulting cache may be different if the same\n    options are not used in both caches.\n\n* `purgeStale()`\n\n    Delete any stale entries.  Returns `true` if anything was removed,\n    `false` otherwise.\n\n### Internal Methods and Properties\n\nDo not use or rely on these.  They will change or be removed without\nnotice.  They will cause undefined behavior if used inappropriately.\n\nThis documentation is here so that it is especially clear that this not\n\"undocumented\" because someone forgot; it _is_ documented, and the\ndocumentation is telling you not to do it.\n\nDo not report bugs that stem from using these properties.  They will be\nignored.\n\n* `setKeyIndex()` Assign an index to a given key.\n* `getKeyIndex()` Get the index for a given key.\n* `deleteKeyIndex()` Remove the index for a given key.\n* `getDisposeData()` Get the data to pass to a `dispose()` call.\n* `callDispose()` Actually call the `dispose()` function.\n* `onSet()` Called to assign data when `set()` is called.\n* `evict()` Delete the least recently used item.\n* `onDelete()` Perform actions required for deleting an entry.\n* `isStale()` Check if an item is stale, by index.\n* `list` The internal linked list of indexes defining recency.\n\n## Performance\n\nAs of January 2022, version 7 of this library is one of the most performant\nLRU cache implementations in JavaScript.\n\nBenchmarks can be extremely difficult to get right.  In particular, the\nperformance of set/get/delete operations on objects will vary _wildly_\ndepending on the type of key used.  V8 is highly optimized for objects with\nkeys that are short strings, especially integer numeric strings.  Thus any\nbenchmark which tests _solely_ using numbers as keys will tend to find that\nan object-based approach performs the best.\n\nNote that coercing _anything_ to strings to use as object keys is unsafe,\nunless you can be 100% certain that no other type of value will be used.\nFor example:\n\n```js\nconst myCache = {}\nconst set = (k, v) => myCache[k] = v\nconst get = (k) => myCache[k]\n\nset({}, 'please hang onto this for me')\nset('[object Object]', 'oopsie')\n```\n\nAlso beware of \"Just So\" stories regarding performance.  Garbage collection\nof large (especially: deep) object graphs can be incredibly costly, with\nseveral \"tipping points\" where it increases exponentially.  As a result,\nputting that off until later can make it much worse, and less predictable.\nIf a library performs well, but only in a scenario where the object graph is\nkept shallow, then that won't help you if you are using large objects as\nkeys.\n\nIn general, when attempting to use a library to improve performance (such\nas a cache like this one), it's best to choose an option that will perform\nwell in the sorts of scenarios where you'll actually use it.\n\nThis library is optimized for repeated gets and minimizing eviction time,\nsince that is the expected need of a LRU.  Set operations are somewhat\nslower on average than a few other options, in part because of that\noptimization.  It is assumed that you'll be caching some costly operation,\nideally as rarely as possible, so optimizing set over get would be unwise.\n\nIf performance matters to you:\n\n1. If it's at all possible to use small integer values as keys, and you can\n   guarantee that no other types of values will be used as keys, then do that,\n   and use a cache such as [lru-fast](https://npmjs.com/package/lru-fast)\n   which uses an Object as its data store.\n2. Failing that, if at all possible, use short non-numeric strings (ie,\n   less than 256 characters) as your keys.\n3. If you know that the types of your keys will be long strings, strings\n   that look like floats, `null`, objects, or some mix of types, then this\n   library will work well for you.\n4. Do not use a `dispose` function, size tracking, or ttl behavior, unless\n   absolutely needed.  These features are convenient, and necessary in some\n   use cases, and every attempt has been made to make the performance\n   impact minimal, but it isn't nothing.\n\n## Breaking Changes in Version 7\n\nThis library changed to a different algorithm and internal data structure\nin version 7, yielding significantly better performance, albeit with\nsome subtle changes as a result.\n\nIf you were relying on the internals of LRUCache in version 6 or before, it\nprobably will not work in version 7 and above.\n\n### Specific API Changes\n\nFor the most part, the feature set has been maintained as much as possible.\n\nHowever, some other cleanup and refactoring changes were made in v7 as\nwell.\n\n* The `set()`, `get()`, and `has()` functions take options objects\n  instead of positional booleans/integers for optional parameters.\n* `size` can be set explicitly on `set()`.\n* `cache.length` was renamed to the more fitting `cache.size`.\n* Option name deprecations:\n  * `stale` -> `allowStale`\n  * `length` -> `sizeCalculation`\n  * `maxAge` -> `ttl`\n* The objects used by `cache.load()` and `cache.dump()` are incompatible\n  with previous versions.\n","readmeFilename":"README.md","gitHead":"e92da0a6af50a6f181eeca48cffaf266e2dd10f0","bugs":{"url":"https://github.com/isaacs/node-lru-cache/issues"},"homepage":"https://github.com/isaacs/node-lru-cache#readme","_id":"lru-cache@7.0.4","_nodeVersion":"17.6.0","_npmVersion":"8.5.3","dist":{"integrity":"sha512-2kmlhulWWdRsG+ELcVC94Gs0MRurw6Y5Gh1EHMzbYnYkXeqNHtGg9PTjlcmnT+gxcihwlYwjJ+/gYQl37hMV5Q==","shasum":"505f3e1dc5b2c0189bff238a1b98dfecfc4aa8dd","tarball":"http://registry-npm.ywwl.com/lru-cache/download/lru-cache-7.0.4.tgz","fileCount":4,"unpackedSize":29758,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD/2ULncTBQFof5SPU3nFYJ1OLxdRA04quMqLHutFVi8QIga9p4O/FgcyUyx4VwgTXnNoJAaXOWjxmBNi5+oPxXEG0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiUeIyACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmptqw/9FQjS+b45ECps5qMEnhfH9nloqBg8ewiIXHIrVws4MKDzXgbC\r\njg4YTGNYubE+Ei5boHAb7XlSqXENBo9K/qmSJSjMCdnuUK7ArMCDtpgvjeJC\r\n+msfA0ohBeh2BqLhxsVgmEXhfVkmQCZRL2uaUgLPpqEDXHiEXlYxZUyTclXw\r\nmSfH1HOwqQhFbG1pIhIzeJKRVtcdXsUw2GkyHCo+ZvLRU5GZWClSNj7D99W0\r\nQw7ixABMRiNHEnkxJat07B1Gvdz4f1f9o+6Eo/80Xww1mhVCJnTcdZysIRsE\r\nSD3Vmf9czeSfXKH7QhzNmmITonP0pBxiYsqusWWAPT2Fi7yEZrcF6W4vhpnC\r\noOH8bMc/S5E7+iQ4ckWDiFry4qdSE/C9+v7W04wlC31blR/3CiwYWIO6D5Xa\r\nUgg5+QcGxYXbpqOmm+ux0zY9IN2X9C8F5RrjZ1KB3xDxO4RyruE6WU13trg/\r\nMwdV/N9ReRINZBksawbs7bIUk3oRNQK4FNXra5NFAeTKQmJYjFIApUi+D4cv\r\nDfc7owH6/5akpaXp12503c5rvG60SbYyHExHIxCD2ErGQVGh0opMB1N8Dbbh\r\n9RK3JB4Fb3fvbIp2k/IzQ5LM0vXxap5FrmEeRoG+LD4NKrZVZGE11jpFzqQN\r\n2SLHNK68yBCTpovynRzj4jXlfO48C0FUAy8=\r\n=w6Gz\r\n-----END PGP SIGNATURE-----\r\n","size":10178,"noattachment":false,"key":"/lru-cache/-/lru-cache-7.0.4.tgz"},"_npmUser":{"name":"isaacs","email":"i@izs.me"},"directories":{},"maintainers":[{"name":"isaacs","email":"i@izs.me"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/lru-cache_7.0.4_1649533489992_0.1583613736748093"},"_hasShrinkwrap":false,"publish_time":1649533490120,"_cnpm_publish_time":1649533490120,"dist-tags":{"latest":"11.5.3","legacy":"4.1.5","legacy-v10":"10.4.3","v7.0-backport":"7.0.4","v7.1-backport":"7.1.3","v7.2-backport":"7.2.3","v7.3-backport":"7.3.3","v7.4-backport":"7.4.5","v7.5-backport":"7.5.2","v7.6-backport":"7.6.1","v7.7-backport":"7.7.4"}}