项目原始demo,不改动
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # mem [![Build Status](https://travis-ci.org/sindresorhus/mem.svg?branch=master)](https://travis-ci.org/sindresorhus/mem)
  2. > [Memoize](https://en.wikipedia.org/wiki/Memoization) functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input
  3. ## Install
  4. ```
  5. $ npm install --save mem
  6. ```
  7. ## Usage
  8. ```js
  9. const mem = require('mem');
  10. let i = 0;
  11. const counter = () => ++i;
  12. const memoized = mem(counter);
  13. memoized('foo');
  14. //=> 1
  15. // cached as it's the same arguments
  16. memoized('foo');
  17. //=> 1
  18. // not cached anymore as the arguments changed
  19. memoized('bar');
  20. //=> 2
  21. memoized('bar');
  22. //=> 2
  23. ```
  24. ##### Works fine with promise returning functions
  25. ```js
  26. const mem = require('mem');
  27. let i = 0;
  28. const counter = () => Promise.resolve(++i);
  29. const memoized = mem(counter);
  30. memoized().then(a => {
  31. console.log(a);
  32. //=> 1
  33. memoized().then(b => {
  34. // the return value didn't increase as it's cached
  35. console.log(b);
  36. //=> 1
  37. });
  38. });
  39. ```
  40. ```js
  41. const mem = require('mem');
  42. const got = require('got');
  43. const memGot = mem(got, {maxAge: 1000});
  44. memGot('sindresorhus.com').then(() => {
  45. // this call is cached
  46. memGot('sindresorhus.com').then(() => {
  47. setTimeout(() => {
  48. // this call is not cached as the cache has expired
  49. memGot('sindresorhus.com').then(() => {});
  50. }, 2000);
  51. });
  52. });
  53. ```
  54. ## API
  55. ### mem(fn, [options])
  56. #### fn
  57. Type: `Function`
  58. Function to be memoized.
  59. #### options
  60. ##### maxAge
  61. Type: `number`<br>
  62. Default: `Infinity`
  63. Milliseconds until the cache expires.
  64. ##### cacheKey
  65. Type: `Function`
  66. Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
  67. You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
  68. ##### cache
  69. Type: `Object`<br>
  70. Default: `new Map()`
  71. Use a different cache storage. Must implement the following methods: `.has(key)`, `.get(key)`, `.set(key, value)`, and optionally `.clear()`. You could for example use a `WeakMap` instead.
  72. ### mem.clear(fn)
  73. Clear all cached data of a memoized function.
  74. #### fn
  75. Type: `Function`
  76. Memoized function.
  77. ## Tips
  78. ### Cache statistics
  79. If you want to know how many times your cache had a hit or a miss, you can make use of [stats-map](https://github.com/SamVerschueren/stats-map) as a replacement for the default cache.
  80. #### Example
  81. ```js
  82. const mem = require('mem');
  83. const StatsMap = require('stats-map');
  84. const got = require('got');
  85. const cache = new StatsMap();
  86. const memGot = mem(got, {cache});
  87. memGot('sindresorhus.com')
  88. .then(() => memGot('sindresorhus.com'))
  89. .then(() => memGot('sindresorhus.com'));
  90. console.log(cache.stats);
  91. //=> {hits: 2, misses: 1}
  92. ```
  93. ## License
  94. MIT © [Sindre Sorhus](https://sindresorhus.com)