{"name":"lru-cache","description":"A cache object that deletes the least-recently-used items.","version":"7.1.2","publishConfig":{"tag":"v7.1-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","tap":"^15.1.6"},"license":"ISC","engines":{"node":">=12"},"tap":{"coverage-map":"map.js"},"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 by default,\n  // and MAY live in the cache, contributing to its LRU max, long after\n  // they 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 by default, and MAY\n  live in the cache, contributing to its LRU max, long after they have\n  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* `ttlResolution` - Minimum amount of time in ms in which to check for\n  staleness.  Defaults to `1`, which means that the current time is checked\n  at most once per millisecond.\n\n    Set to `0` to check the current time every time staleness is tested.\n\n    Note that setting this to a higher value _will_ improve performance\n    somewhat while using ttl tracking, albeit at the expense of keeping\n    stale items around a bit longer than intended.\n\n* `ttlAutopurge` - Preemptively remove stale items from the cache.\n\n    Note that this may _significantly_ degrade performance, especially if\n    the cache is storing a large number of items.  It is almost always best\n    to just leave the stale items in the cache, and let them fall out as\n    new items are added.\n\n    Note that this means that `allowStale` is a bit pointless, as stale\n    items will be deleted almost as soon as they expire.\n\n    Use with caution!\n\n    Boolean, default `false`\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  preemptively 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.maxSize`, `cache.allowStale`, `cache.noDisposeOnSet`,\n  `cache.sizeCalculation`, `cache.dispose`, `cache.maxSize`, `cache.ttl`,\n  `cache.updateAgeOnGet`\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    Deprecated alias: `prune`\n\n* `forEach(fn, [thisp])`\n\n    Call the `fn` function with each set of `fn(value, key, cache)` in the\n    LRU cache, from most recent to least recently used.\n\n    Does not affect recency of use.\n\n    If `thisp` is provided, function will be called in the `this`-context\n    of the provided object.\n\n* `rforEach(fn, [thisp])`\n\n    Same as `cache.forEach(fn, thisp)`, but in order from least recently\n    used to most recently used.\n\n* `pop()`\n\n    Evict the least recently used item, returning its value.\n\n    Returns `undefined` if cache is empty.\n\n### Internal Methods and Properties\n\nIn order to optimize performance as much as possible, \"private\" members and\nmethods are exposed on the object as normal properties, rather than being\naccessed via Symbols, private members, or closure variables.\n\n**Do not use or rely on these.**  They will change or be removed without\nnotice.  They will cause undefined behavior if used inappropriately.  There\nis no need or reason to ever call them directly.\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\n**Do not report bugs that stem from using these properties.**  They will be\nignored.\n\n* `initializeTTLTracking()` Set up the cache for tracking TTLs\n* `updateItemAge(index)` Called when an item age is updated, by internal ID\n* `setItemTTL(index)` Called when an item ttl is updated, by internal ID\n* `isStale(index)` Called to check an item's staleness, by internal ID\n* `initializeSizeTracking()` Set up the cache for tracking item size.\n  Called automatically when a size is specified.\n* `removeItemSize(index)` Updates the internal size calculation when an\n  item is removed or modified, by internal ID\n* `addItemSize(index)` Updates the internal size calculation when an item\n  is added or modified, by internal ID\n* `indexes()` An iterator over the non-stale internal IDs, from most\n  recently to least recently used.\n* `rindexes()` An iterator over the non-stale internal IDs, from least\n  recently to most recently used.\n* `newIndex()` Create a new internal ID, either reusing a deleted ID,\n  evicting the least recently used ID, or walking to the end of the\n  allotted space.\n* `evict()` Evict the least recently used internal ID, returning its ID.\n  Does not do any bounds checking.\n* `connect(p, n)` Connect the `p` and `n` internal IDs in the linked list.\n* `moveToTail(index)` Move the specified internal ID to the most recently\n  used position.\n* `keyMap` Map of keys to internal IDs\n* `keyList` List of keys by internal ID\n* `valList` List of values by internal ID\n* `sizes` List of calculated sizes by internal ID\n* `ttls` List of TTL values by internal ID\n* `starts` List of start time values by internal ID\n* `next` Array of \"next\" pointers by internal ID\n* `prev` Array of \"previous\" pointers by internal ID\n* `head` Internal ID of least recently used item\n* `tail` Internal ID of most recently used item\n* `free` Stack of deleted internal IDs\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\n   that, and use a cache such as\n   [lru-fast](https://npmjs.com/package/lru-fast), or [mnemonist's\n   LRUCache](https://yomguithereal.github.io/mnemonist/lru-cache) which\n   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, and use [mnemonist's\n   LRUCache](https://yomguithereal.github.io/mnemonist/lru-cache).\n3. If the types of your keys will be long strings, strings that look like\n   floats, `null`, objects, or some mix of types, or if you aren't sure,\n   then this library will work well for you.\n4. Do not use a `dispose` function, size tracking, or especially ttl\n   behavior, unless absolutely needed.  These features are convenient, and\n   necessary in some use cases, and every attempt has been made to make the\n   performance 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\nFor more info, see the [change log](CHANGELOG.md).\n","readmeFilename":"README.md","gitHead":"440baeca180e03581e50c37ae873fa862d73a197","bugs":{"url":"https://github.com/isaacs/node-lru-cache/issues"},"homepage":"https://github.com/isaacs/node-lru-cache#readme","_id":"lru-cache@7.1.2","_nodeVersion":"17.6.0","_npmVersion":"8.5.3","dist":{"integrity":"sha512-u4Lv1LM66bMGCi8WQUURP7ORjC0JNlw0jrJZr/0prh4SOcTD68sZSECgJ2xtjM/PO0/Y5NUGAP43EeCaOByCAA==","shasum":"ab90020ba299d9d140cf97570958ec67eb3f2797","tarball":"http://registry-npm.ywwl.com/lru-cache/download/lru-cache-7.1.2.tgz","fileCount":4,"unpackedSize":33937,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJiKlKqACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmogmw/9EMq7Gziii7rv2sLlAZwcijllKU+SPwkUlIKGwnb8kZTILEjp\r\nwE8jI6FWqCWGa0g4Mr7KEsEOtHbLGZ7QIfjs0iDHzvNsuuIXplfL4JduHTv6\r\n/pETfmyNySuGPiUJyEN1kcxiuTSNXtO0SZ8RUJthH+EgpInpMzqMrV5zTtOA\r\nHlfBKuc8tCvOyZnRxyFWhCO5KaJ2RayJNp4UuaDa8MiJ2RMxOVrnG12HA89o\r\n4us8iW4M/M0HYWiR9Wlap/PKByYIz4XRxItgg6+xZyzSTY8T+OFxLNdK1VCA\r\nC8P/b0bhPLm3EP8WAJdU+8o8ZjadyRcwr9z8AoZmIA9UpNLu30uZ6ANV4hVX\r\n3P9wAONTML6QwLHLyDRo0ZHGlRSDnrSIiXfs4nPeBCsuw9dkfwpha6rkn8oS\r\nZNDELTxWgCCCGC0GXBBLOXTaGjxh1vBKo53m/7MsqC+uxNHgGPRjPKvp/Uao\r\ni1U/w6/DgmdyTfXNbJvtntuqD5PGRMQYp7Djxdn1cFgA1+eqiWZCV8Ugxjz+\r\nGrwfafV9L3w25rRMVq31FMUAqUqa3UBmHRowCdcllyP7EImk+HRQaqPCpqoC\r\nZifTsmqO9X4M8k7S0v8LIo0dhErn18lOdk7hiFbfUCR3cbT0CDYx1u9bJODW\r\nSVlNFtV+grXlQxquAdw7a+30xX49HWdIBgw=\r\n=jaIx\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHdFdPYNa0ihT7hvbvABQbiMZjd2o1NAb7d/ylmO7+t2AiBY601rZ6gYrBZWbPzusNGmYR3cIE2vDFUAZEGkKDkiMw=="}],"size":11147,"noattachment":false,"key":"/lru-cache/-/lru-cache-7.1.2.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.1.2_1646940841899_0.6806455451382682"},"_hasShrinkwrap":false,"deprecated":"Please update to latest patch version to fix memory leak https://github.com/isaacs/node-lru-cache/issues/227","publish_time":1646940842046,"_cnpm_publish_time":1646940842046,"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"}}