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.

111 lines
2.6 KiB

2 years ago
  1. # quick-lru [![Build Status](https://travis-ci.org/sindresorhus/quick-lru.svg?branch=master)](https://travis-ci.org/sindresorhus/quick-lru) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/quick-lru/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/quick-lru?branch=master)
  2. > Simple [“Least Recently Used” (LRU) cache](https://en.m.wikipedia.org/wiki/Cache_replacement_policies#Least_Recently_Used_.28LRU.29)
  3. Useful when you need to cache something and limit memory usage.
  4. Inspired by the [`hashlru` algorithm](https://github.com/dominictarr/hashlru#algorithm), but instead uses [`Map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map) to support keys of any type, not just strings, and values can be `undefined`.
  5. ## Install
  6. ```
  7. $ npm install quick-lru
  8. ```
  9. ## Usage
  10. ```js
  11. const QuickLRU = require('quick-lru');
  12. const lru = new QuickLRU({maxSize: 1000});
  13. lru.set('🦄', '🌈');
  14. lru.has('🦄');
  15. //=> true
  16. lru.get('🦄');
  17. //=> '🌈'
  18. ```
  19. ## API
  20. ### new QuickLRU(options?)
  21. Returns a new instance.
  22. ### options
  23. Type: `object`
  24. #### maxSize
  25. *Required*\
  26. Type: `number`
  27. The maximum number of items before evicting the least recently used items.
  28. #### onEviction
  29. *Optional*\
  30. Type: `(key, value) => void`
  31. Called right before an item is evicted from the cache.
  32. Useful for side effects or for items like object URLs that need explicit cleanup (`revokeObjectURL`).
  33. ### Instance
  34. The instance is [`iterable`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Iteration_protocols) so you can use it directly in a [`for…of`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...of) loop.
  35. Both `key` and `value` can be of any type.
  36. #### .set(key, value)
  37. Set an item. Returns the instance.
  38. #### .get(key)
  39. Get an item.
  40. #### .has(key)
  41. Check if an item exists.
  42. #### .peek(key)
  43. Get an item without marking it as recently used.
  44. #### .delete(key)
  45. Delete an item.
  46. Returns `true` if the item is removed or `false` if the item doesn't exist.
  47. #### .clear()
  48. Delete all items.
  49. #### .keys()
  50. Iterable for all the keys.
  51. #### .values()
  52. Iterable for all the values.
  53. #### .size
  54. The stored item count.
  55. ---
  56. <div align="center">
  57. <b>
  58. <a href="https://tidelift.com/subscription/pkg/npm-quick-lru?utm_source=npm-quick-lru&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  59. </b>
  60. <br>
  61. <sub>
  62. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  63. </sub>
  64. </div>