You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # lru cache
  2. A cache object that deletes the least-recently-used items.
  3. [![Build Status](https://travis-ci.org/isaacs/node-lru-cache.svg?branch=master)](https://travis-ci.org/isaacs/node-lru-cache) [![Coverage Status](https://coveralls.io/repos/isaacs/node-lru-cache/badge.svg?service=github)](https://coveralls.io/github/isaacs/node-lru-cache)
  4. ## Installation:
  5. ```javascript
  6. npm install lru-cache --save
  7. ```
  8. ## Usage:
  9. ```javascript
  10. var LRU = require("lru-cache")
  11. , options = { max: 500
  12. , length: function (n, key) { return n * 2 + key.length }
  13. , dispose: function (key, n) { n.close() }
  14. , maxAge: 1000 * 60 * 60 }
  15. , cache = LRU(options)
  16. , otherCache = LRU(50) // sets just the max size
  17. cache.set("key", "value")
  18. cache.get("key") // "value"
  19. // non-string keys ARE fully supported
  20. // but note that it must be THE SAME object, not
  21. // just a JSON-equivalent object.
  22. var someObject = { a: 1 }
  23. cache.set(someObject, 'a value')
  24. // Object keys are not toString()-ed
  25. cache.set('[object Object]', 'a different value')
  26. assert.equal(cache.get(someObject), 'a value')
  27. // A similar object with same keys/values won't work,
  28. // because it's a different object identity
  29. assert.equal(cache.get({ a: 1 }), undefined)
  30. cache.reset() // empty the cache
  31. ```
  32. If you put more stuff in it, then items will fall out.
  33. If you try to put an oversized thing in it, then it'll fall out right
  34. away.
  35. ## Options
  36. * `max` The maximum size of the cache, checked by applying the length
  37. function to all values in the cache. Not setting this is kind of
  38. silly, since that's the whole purpose of this lib, but it defaults
  39. to `Infinity`.
  40. * `maxAge` Maximum age in ms. Items are not pro-actively pruned out
  41. as they age, but if you try to get an item that is too old, it'll
  42. drop it and return undefined instead of giving it to you.
  43. * `length` Function that is used to calculate the length of stored
  44. items. If you're storing strings or buffers, then you probably want
  45. to do something like `function(n, key){return n.length}`. The default is
  46. `function(){return 1}`, which is fine if you want to store `max`
  47. like-sized things. The item is passed as the first argument, and
  48. the key is passed as the second argumnet.
  49. * `dispose` Function that is called on items when they are dropped
  50. from the cache. This can be handy if you want to close file
  51. descriptors or do other cleanup tasks when items are no longer
  52. accessible. Called with `key, value`. It's called *before*
  53. actually removing the item from the internal cache, so if you want
  54. to immediately put it back in, you'll have to do that in a
  55. `nextTick` or `setTimeout` callback or it won't do anything.
  56. * `stale` By default, if you set a `maxAge`, it'll only actually pull
  57. stale items out of the cache when you `get(key)`. (That is, it's
  58. not pre-emptively doing a `setTimeout` or anything.) If you set
  59. `stale:true`, it'll return the stale value before deleting it. If
  60. you don't set this, then it'll return `undefined` when you try to
  61. get a stale entry, as if it had already been deleted.
  62. * `noDisposeOnSet` By default, if you set a `dispose()` method, then
  63. it'll be called whenever a `set()` operation overwrites an existing
  64. key. If you set this option, `dispose()` will only be called when a
  65. key falls out of the cache, not when it is overwritten.
  66. ## API
  67. * `set(key, value, maxAge)`
  68. * `get(key) => value`
  69. Both of these will update the "recently used"-ness of the key.
  70. They do what you think. `maxAge` is optional and overrides the
  71. cache `maxAge` option if provided.
  72. If the key is not found, `get()` will return `undefined`.
  73. The key and val can be any value.
  74. * `peek(key)`
  75. Returns the key value (or `undefined` if not found) without
  76. updating the "recently used"-ness of the key.
  77. (If you find yourself using this a lot, you *might* be using the
  78. wrong sort of data structure, but there are some use cases where
  79. it's handy.)
  80. * `del(key)`
  81. Deletes a key out of the cache.
  82. * `reset()`
  83. Clear the cache entirely, throwing away all values.
  84. * `has(key)`
  85. Check if a key is in the cache, without updating the recent-ness
  86. or deleting it for being stale.
  87. * `forEach(function(value,key,cache), [thisp])`
  88. Just like `Array.prototype.forEach`. Iterates over all the keys
  89. in the cache, in order of recent-ness. (Ie, more recently used
  90. items are iterated over first.)
  91. * `rforEach(function(value,key,cache), [thisp])`
  92. The same as `cache.forEach(...)` but items are iterated over in
  93. reverse order. (ie, less recently used items are iterated over
  94. first.)
  95. * `keys()`
  96. Return an array of the keys in the cache.
  97. * `values()`
  98. Return an array of the values in the cache.
  99. * `length`
  100. Return total length of objects in cache taking into account
  101. `length` options function.
  102. * `itemCount`
  103. Return total quantity of objects currently in cache. Note, that
  104. `stale` (see options) items are returned as part of this item
  105. count.
  106. * `dump()`
  107. Return an array of the cache entries ready for serialization and usage
  108. with 'destinationCache.load(arr)`.
  109. * `load(cacheEntriesArray)`
  110. Loads another cache entries array, obtained with `sourceCache.dump()`,
  111. into the cache. The destination cache is reset before loading new entries
  112. * `prune()`
  113. Manually iterates over the entire cache proactively pruning old entries