Skip to content

Commit adb17d0

Browse files
authored
Merge pull request groupon#39 from aaarichter/delay-writing
feat: drop node4 support
2 parents 23de763 + 23dcc6a commit adb17d0

File tree

10 files changed

+49
-45
lines changed

10 files changed

+49
-45
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "groupon/node4"
2+
"extends": "groupon/node6"
33
}

.travis.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
language: node_js
22
node_js:
3-
- 4.6.1
4-
- 6.11.5
5-
- 8.9.0
3+
- v6.14.3
4+
- v8.11.3
5+
- v10.5.0
66
deploy:
77
- provider: script
88
script: ./node_modules/.bin/nlm release
99
skip_cleanup: true
1010
'on':
1111
branch: master
12-
node: 8.9.0
12+
node: 10.5.0
1313
services: memcached

lib/backend.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
'use strict';
3434

35-
const typeMap = Object.create(null);
35+
const typeMap = new Map();
3636

3737
function isBackend(object) {
3838
return typeof object.get === 'function' && typeof object.set === 'function';
@@ -46,15 +46,15 @@ exports.create = function create(options) {
4646
}
4747

4848
const type = options.type || 'noop';
49-
const BackendClass = typeMap[type];
49+
const BackendClass = typeMap.get(type);
5050
if (!BackendClass) {
5151
throw new Error(`${type} is not a supported cache backend type`);
5252
}
5353
return new BackendClass(options);
5454
};
5555

5656
function addType(type, BackendClass) {
57-
typeMap[type] = BackendClass;
57+
typeMap.set(type, BackendClass);
5858
return BackendClass;
5959
}
6060
exports.addType = addType;

lib/backends/memcached.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
'use strict';
3434

3535
const promisify = require('bluebird').promisify;
36-
const _ = require('lodash');
3736
const Memcached = require('memcached');
3837

38+
const noop = () => undefined;
39+
3940
function createClient(options) {
4041
if (options.client) {
4142
return options.client;
@@ -63,11 +64,12 @@ MemcachedBackend.prototype.get = function get(key) {
6364
};
6465

6566
MemcachedBackend.prototype.set = function set(key, value, options) {
66-
return this._clientSet(key, value, options.expire).then(_.constant(value));
67+
const constant = () => value;
68+
return this._clientSet(key, value, options.expire).then(constant);
6769
};
6870

6971
MemcachedBackend.prototype.unset = function unset(key) {
70-
return this._clientDel(key).then(_.noop);
72+
return this._clientDel(key).then(noop);
7173
};
7274

7375
MemcachedBackend.prototype.end = function end() {

lib/backends/memory.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,29 @@ const util = require('../util');
3838

3939
/* Stores everything just in memory */
4040
function MemoryBackend() {
41-
this.cache = Object.create(null);
41+
this.cache = new Map();
4242
this.type = 'memory';
4343
}
4444
module.exports = MemoryBackend;
4545

4646
MemoryBackend.prototype.get = function get(key) {
47-
let wrappedValue = this.cache[key] || null;
47+
let wrappedValue = this.cache.get(key) || null;
4848
if (util.isExpired(wrappedValue && wrappedValue.e)) {
4949
wrappedValue = null;
50-
delete this.cache[key];
50+
this.cache.delete(key);
5151
}
5252
return Bluebird.resolve(wrappedValue ? wrappedValue.d : null);
5353
};
5454

5555
MemoryBackend.prototype.set = function set(key, value, options) {
56-
this.cache[key] = {
56+
this.cache.set(key, {
5757
d: value,
5858
e: util.expiresAt(options.expire),
59-
};
59+
});
6060
return Bluebird.resolve(value);
6161
};
6262

6363
MemoryBackend.prototype.unset = function unset(key) {
64-
delete this.cache[key];
64+
this.cache.delete(key);
6565
return Bluebird.resolve();
6666
};

lib/cache.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
'use strict';
3636

37-
const _ = require('lodash');
38-
3937
const Backend = require('./backend');
4038
const getOrElse = require('./get-or-else');
4139
const util = require('./util');
@@ -81,7 +79,7 @@ Cache.prototype.end = function end() {
8179
};
8280

8381
Cache.prototype.prepareOptions = function prepareOptions(options) {
84-
return _.extend({}, this.defaults, options);
82+
return Object.assign({}, this.defaults, options);
8583
};
8684

8785
Cache.prototype._set = function _set(key, val, options) {
@@ -119,6 +117,7 @@ Cache.prototype._applyTimeout = function _applyTimeout(value) {
119117
Cache.prototype._getWrapped = function _getWrapped(key) {
120118
return this._applyTimeout(this.backend.get(key));
121119
};
120+
122121
// For backwards compatibility, eventually we should deprecate this.
123122
// It *should* be a private API.
124123
Cache.prototype.getWrapped = Cache.prototype._getWrapped;

lib/cached.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,36 @@
3333
'use strict';
3434

3535
const Bluebird = require('bluebird');
36-
const _ = require('lodash');
3736
const util = require('util');
3837

3938
const Cache = require('./cache');
4039

41-
let namedCaches = Object.create(null);
40+
const namedCaches = new Map();
4241

43-
const getName = util.deprecate(function getName(name) {
44-
return name || 'default';
45-
}, 'Unnamed caches and caches with non-string names are deprecated.');
42+
const getName = util.deprecate(
43+
name => name || 'default',
44+
'Unnamed caches and caches with non-string names are deprecated.'
45+
);
4646

4747
function cached(name, options) {
4848
if (typeof name !== 'string') {
4949
name = getName(name);
5050
}
5151

52-
if (!(name in namedCaches)) {
53-
namedCaches[name] = cached.createCache(
54-
_.extend(
55-
{
56-
name: name,
57-
},
58-
options || {}
52+
if (!namedCaches.has(name)) {
53+
namedCaches.set(
54+
name,
55+
cached.createCache(
56+
Object.assign(
57+
{
58+
name: name,
59+
},
60+
options || {}
61+
)
5962
)
6063
);
6164
}
62-
return namedCaches[name];
65+
return namedCaches.get(name);
6366
}
6467
module.exports = cached;
6568

@@ -68,17 +71,17 @@ cached.createCache = function createCache(options) {
6871
};
6972

7073
cached.dropNamedCaches = function dropNamedCaches() {
71-
namedCaches = Object.create(null);
74+
namedCaches.clear();
7275
return cached;
7376
};
7477

7578
cached.dropNamedCache = function dropNamedCache(name) {
76-
delete namedCaches[name];
79+
namedCaches.delete(name);
7780
return cached;
7881
};
7982

8083
cached.knownCaches = function knownCaches() {
81-
return Object.keys(namedCaches);
84+
return [...namedCaches.keys()];
8285
};
8386

8487
cached.deferred = function deferred(fn) {

lib/get-or-else.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
'use strict';
3636

3737
const Bluebird = require('bluebird');
38-
const _ = require('lodash');
3938

4039
const util = require('./util');
4140

@@ -77,10 +76,12 @@ function getOrElse(cache, key, val, opts) {
7776

7877
function verifyFreshness(wrappedValue) {
7978
const hit = !!wrappedValue;
80-
const expired = util.isExpired(wrappedValue && wrappedValue.b);
8179
let loadingNewValue = cache.staleOrPending[key] !== undefined;
8280

83-
if ((!hit || expired) && !loadingNewValue) {
81+
if (
82+
!loadingNewValue &&
83+
(!hit || util.isExpired(wrappedValue && wrappedValue.b))
84+
) {
8485
cache.staleOrPending[key] = refreshValue();
8586
loadingNewValue = true;
8687
}
@@ -96,7 +97,7 @@ function getOrElse(cache, key, val, opts) {
9697

9798
return cache
9899
._getWrapped(key)
99-
.catch(_.constant(null))
100+
.catch(() => null)
100101
.then(verifyFreshness);
101102
}
102103
module.exports = getOrElse;

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
},
2828
"dependencies": {
2929
"bluebird": "^3.3.3",
30-
"lodash": "^4.6.1",
31-
"memcached": "^2.1.0"
30+
"memcached": "^2.2.2"
3231
},
3332
"devDependencies": {
3433
"assertive": "^2.1.0",
@@ -41,8 +40,8 @@
4140
"eslint-plugin-import": "^2.8.0",
4241
"eslint-plugin-node": "^5.2.1",
4342
"eslint-plugin-prettier": "^2.2.0",
44-
"mocha": "^3.1.2",
45-
"nlm": "^3.0.0",
43+
"mocha": "^5.2.0",
44+
"nlm": "^3.3.1",
4645
"prettier": "^1.6.1"
4746
},
4847
"author": {

test/timeout.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'assertive';
22
import Bluebird from 'bluebird';
3-
import { identity } from 'lodash';
3+
const identity = val => val;
44

55
import Cache from '../lib/cache';
66

0 commit comments

Comments
 (0)