Skip to content

Commit

Permalink
Require Node.js 6
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 20, 2018
1 parent 16ff264 commit 77874ca
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 80 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
35 changes: 21 additions & 14 deletions index.js
Expand Up @@ -4,47 +4,54 @@ const isPromise = require('p-is-promise');

const cacheStore = new WeakMap();

const defaultCacheKey = function (x) {
if (arguments.length === 1 && (x === null || x === undefined || (typeof x !== 'function' && typeof x !== 'object'))) {
return x;
const defaultCacheKey = (...args) => {
if (args.length === 1) {
const [firstArgument] = args;
if (
firstArgument === null ||
firstArgument === undefined ||
(typeof firstArgument !== 'function' && typeof firstArgument !== 'object')
) {
return firstArgument;
}
}

return JSON.stringify(arguments);
return JSON.stringify(args);
};

module.exports = (fn, opts) => {
opts = Object.assign({
module.exports = (fn, options) => {
options = Object.assign({
cacheKey: defaultCacheKey,
cache: new Map(),
cachePromiseRejection: false
}, opts);
}, options);

const memoized = function () {
const memoized = function (...args) {
const cache = cacheStore.get(memoized);
const key = opts.cacheKey.apply(null, arguments);
const key = options.cacheKey(...args);

if (cache.has(key)) {
const c = cache.get(key);

if (typeof opts.maxAge !== 'number' || Date.now() < c.maxAge) {
if (typeof options.maxAge !== 'number' || Date.now() < c.maxAge) {
return c.data;
}

cache.delete(key);
}

const ret = fn.apply(this, arguments);
const ret = fn.call(this, ...args);

const setData = (key, data) => {
cache.set(key, {
data,
maxAge: Date.now() + (opts.maxAge || 0)
maxAge: Date.now() + (options.maxAge || 0)
});
};

setData(key, ret);

if (isPromise(ret) && opts.cachePromiseRejection === false) {
if (isPromise(ret) && options.cachePromiseRejection === false) {
// Remove rejected promises from cache unless `cachePromiseRejection` is set to `true`
ret.catch(() => cache.delete(key));
}
Expand All @@ -54,7 +61,7 @@ module.exports = (fn, opts) => {

mimicFn(memoized, fn);

cacheStore.set(memoized, opts.cache);
cacheStore.set(memoized, options.cache);

return memoized;
};
Expand Down
82 changes: 41 additions & 41 deletions package.json
@@ -1,43 +1,43 @@
{
"name": "mem",
"version": "3.0.1",
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",
"license": "MIT",
"repository": "sindresorhus/mem",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"memoize",
"function",
"mem",
"memoization",
"cache",
"caching",
"optimize",
"performance",
"ttl",
"expire",
"promise"
],
"dependencies": {
"mimic-fn": "^1.0.0",
"p-is-promise": "^1.1.0"
},
"devDependencies": {
"ava": "*",
"delay": "^2.0.0",
"xo": "*"
}
"name": "mem",
"version": "3.0.1",
"description": "Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input",
"license": "MIT",
"repository": "sindresorhus/mem",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"memoize",
"function",
"mem",
"memoization",
"cache",
"caching",
"optimize",
"performance",
"ttl",
"expire",
"promise"
],
"dependencies": {
"mimic-fn": "^1.0.0",
"p-is-promise": "^1.1.0"
},
"devDependencies": {
"ava": "*",
"delay": "^3.0.0",
"xo": "*"
}
}
50 changes: 28 additions & 22 deletions readme.md
Expand Up @@ -40,35 +40,37 @@ memoized('bar');
const mem = require('mem');

let i = 0;
const counter = () => Promise.resolve(++i);
const counter = async () => ++i;
const memoized = mem(counter);

memoized().then(a => {
console.log(a);
(async () => {
console.log(await memoized());
//=> 1

memoized().then(b => {
// The return value didn't increase as it's cached
console.log(b);
//=> 1
});
});
// The return value didn't increase as it's cached
console.log(await memoized());
//=> 1
})();
```

```js
const mem = require('mem');
const got = require('got');
const delay = require('delay');

const memGot = mem(got, {maxAge: 1000});

memGot('sindresorhus.com').then(() => {
(async () => {
await memGot('sindresorhus.com');

// This call is cached
memGot('sindresorhus.com').then(() => {
setTimeout(() => {
// This call is not cached as the cache has expired
memGot('sindresorhus.com').then(() => {});
}, 2000);
});
});
await memGot('sindresorhus.com');

await delay(2000);

// This call is not cached as the cache has expired
await memGot('sindresorhus.com');
})();
```


Expand All @@ -84,6 +86,8 @@ Function to be memoized.

#### options

Type: `Object`

##### maxAge

Type: `number`<br>
Expand Down Expand Up @@ -140,12 +144,14 @@ const got = require('got');
const cache = new StatsMap();
const memGot = mem(got, {cache});

memGot('sindresorhus.com')
.then(() => memGot('sindresorhus.com'))
.then(() => memGot('sindresorhus.com'));
(async () => {
await memGot('sindresorhus.com');
await memGot('sindresorhus.com');
await memGot('sindresorhus.com');

console.log(cache.stats);
//=> {hits: 2, misses: 1}
console.log(cache.stats);
//=> {hits: 2, misses: 1}
})();
```


Expand Down

0 comments on commit 77874ca

Please sign in to comment.