From 29b26fa240033cd7b5409407ea42b375a29c4c33 Mon Sep 17 00:00:00 2001 From: Andreas Richter Date: Sat, 8 Sep 2018 16:29:44 -0500 Subject: [PATCH 1/2] feat: drop node4 support Changes: - [x] drop node4 support - [x] use Map API instead of object hash maps - [x] remove lodash dependency --- .eslintrc | 2 +- .travis.yml | 8 ++++---- lib/backend.js | 6 +++--- lib/backends/memcached.js | 5 ++--- lib/backends/memory.js | 12 ++++++------ lib/cache.js | 5 ++--- lib/cached.js | 35 +++++++++++++++++++---------------- lib/get-or-else.js | 9 +++++---- package.json | 7 +++---- test/timeout.test.js | 2 +- 10 files changed, 46 insertions(+), 45 deletions(-) diff --git a/.eslintrc b/.eslintrc index 5248af3..ca3c940 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,3 @@ { - "extends": "groupon/node4" + "extends": "groupon/node6" } diff --git a/.travis.yml b/.travis.yml index c6c63bd..7a1bee6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: node_js node_js: - - 4.6.1 - - 6.11.5 - - 8.9.0 + - v6.14.3 + - v8.11.3 + - v10.5.0 deploy: - provider: script script: ./node_modules/.bin/nlm release skip_cleanup: true 'on': branch: master - node: 8.9.0 + node: 10.5.0 services: memcached diff --git a/lib/backend.js b/lib/backend.js index 1255d25..ef6b81d 100644 --- a/lib/backend.js +++ b/lib/backend.js @@ -32,7 +32,7 @@ 'use strict'; -const typeMap = Object.create(null); +const typeMap = new Map(); function isBackend(object) { return typeof object.get === 'function' && typeof object.set === 'function'; @@ -46,7 +46,7 @@ exports.create = function create(options) { } const type = options.type || 'noop'; - const BackendClass = typeMap[type]; + const BackendClass = typeMap.get(type); if (!BackendClass) { throw new Error(`${type} is not a supported cache backend type`); } @@ -54,7 +54,7 @@ exports.create = function create(options) { }; function addType(type, BackendClass) { - typeMap[type] = BackendClass; + typeMap.set(type, BackendClass); return BackendClass; } exports.addType = addType; diff --git a/lib/backends/memcached.js b/lib/backends/memcached.js index 8b03694..e12ca9c 100644 --- a/lib/backends/memcached.js +++ b/lib/backends/memcached.js @@ -33,7 +33,6 @@ 'use strict'; const promisify = require('bluebird').promisify; -const _ = require('lodash'); const Memcached = require('memcached'); function createClient(options) { @@ -63,11 +62,11 @@ MemcachedBackend.prototype.get = function get(key) { }; MemcachedBackend.prototype.set = function set(key, value, options) { - return this._clientSet(key, value, options.expire).then(_.constant(value)); + return this._clientSet(key, value, options.expire).then(val => val); }; MemcachedBackend.prototype.unset = function unset(key) { - return this._clientDel(key).then(_.noop); + return this._clientDel(key).then(() => undefined); }; MemcachedBackend.prototype.end = function end() { diff --git a/lib/backends/memory.js b/lib/backends/memory.js index 2f3bc0e..2c2c5bf 100644 --- a/lib/backends/memory.js +++ b/lib/backends/memory.js @@ -38,29 +38,29 @@ const util = require('../util'); /* Stores everything just in memory */ function MemoryBackend() { - this.cache = Object.create(null); + this.cache = new Map(); this.type = 'memory'; } module.exports = MemoryBackend; MemoryBackend.prototype.get = function get(key) { - let wrappedValue = this.cache[key] || null; + let wrappedValue = this.cache.get(key) || null; if (util.isExpired(wrappedValue && wrappedValue.e)) { wrappedValue = null; - delete this.cache[key]; + this.cache.delete(key); } return Bluebird.resolve(wrappedValue ? wrappedValue.d : null); }; MemoryBackend.prototype.set = function set(key, value, options) { - this.cache[key] = { + this.cache.set(key, { d: value, e: util.expiresAt(options.expire), - }; + }); return Bluebird.resolve(value); }; MemoryBackend.prototype.unset = function unset(key) { - delete this.cache[key]; + this.cache.delete(key); return Bluebird.resolve(); }; diff --git a/lib/cache.js b/lib/cache.js index 530c1f4..43378fb 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -34,8 +34,6 @@ 'use strict'; -const _ = require('lodash'); - const Backend = require('./backend'); const getOrElse = require('./get-or-else'); const util = require('./util'); @@ -81,7 +79,7 @@ Cache.prototype.end = function end() { }; Cache.prototype.prepareOptions = function prepareOptions(options) { - return _.extend({}, this.defaults, options); + return Object.assign({}, this.defaults, options); }; Cache.prototype._set = function _set(key, val, options) { @@ -119,6 +117,7 @@ Cache.prototype._applyTimeout = function _applyTimeout(value) { Cache.prototype._getWrapped = function _getWrapped(key) { return this._applyTimeout(this.backend.get(key)); }; + // For backwards compatibility, eventually we should deprecate this. // It *should* be a private API. Cache.prototype.getWrapped = Cache.prototype._getWrapped; diff --git a/lib/cached.js b/lib/cached.js index 95779b6..a39dc4e 100644 --- a/lib/cached.js +++ b/lib/cached.js @@ -33,33 +33,36 @@ 'use strict'; const Bluebird = require('bluebird'); -const _ = require('lodash'); const util = require('util'); const Cache = require('./cache'); -let namedCaches = Object.create(null); +const namedCaches = new Map(); -const getName = util.deprecate(function getName(name) { - return name || 'default'; -}, 'Unnamed caches and caches with non-string names are deprecated.'); +const getName = util.deprecate( + name => name || 'default', + 'Unnamed caches and caches with non-string names are deprecated.' +); function cached(name, options) { if (typeof name !== 'string') { name = getName(name); } - if (!(name in namedCaches)) { - namedCaches[name] = cached.createCache( - _.extend( - { - name: name, - }, - options || {} + if (!namedCaches.has(name)) { + namedCaches.set( + name, + cached.createCache( + Object.assign( + { + name: name, + }, + options || {} + ) ) ); } - return namedCaches[name]; + return namedCaches.get(name); } module.exports = cached; @@ -68,17 +71,17 @@ cached.createCache = function createCache(options) { }; cached.dropNamedCaches = function dropNamedCaches() { - namedCaches = Object.create(null); + namedCaches.clear(); return cached; }; cached.dropNamedCache = function dropNamedCache(name) { - delete namedCaches[name]; + namedCaches.delete(name); return cached; }; cached.knownCaches = function knownCaches() { - return Object.keys(namedCaches); + return [...namedCaches.keys()]; }; cached.deferred = function deferred(fn) { diff --git a/lib/get-or-else.js b/lib/get-or-else.js index f8c55f1..724592b 100644 --- a/lib/get-or-else.js +++ b/lib/get-or-else.js @@ -35,7 +35,6 @@ 'use strict'; const Bluebird = require('bluebird'); -const _ = require('lodash'); const util = require('./util'); @@ -77,10 +76,12 @@ function getOrElse(cache, key, val, opts) { function verifyFreshness(wrappedValue) { const hit = !!wrappedValue; - const expired = util.isExpired(wrappedValue && wrappedValue.b); let loadingNewValue = cache.staleOrPending[key] !== undefined; - if ((!hit || expired) && !loadingNewValue) { + if ( + !loadingNewValue && + (!hit || util.isExpired(wrappedValue && wrappedValue.b)) + ) { cache.staleOrPending[key] = refreshValue(); loadingNewValue = true; } @@ -96,7 +97,7 @@ function getOrElse(cache, key, val, opts) { return cache ._getWrapped(key) - .catch(_.constant(null)) + .catch(() => null) .then(verifyFreshness); } module.exports = getOrElse; diff --git a/package.json b/package.json index 253e57e..ebfe649 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ }, "dependencies": { "bluebird": "^3.3.3", - "lodash": "^4.6.1", - "memcached": "^2.1.0" + "memcached": "^2.2.2" }, "devDependencies": { "assertive": "^2.1.0", @@ -41,8 +40,8 @@ "eslint-plugin-import": "^2.8.0", "eslint-plugin-node": "^5.2.1", "eslint-plugin-prettier": "^2.2.0", - "mocha": "^3.1.2", - "nlm": "^3.0.0", + "mocha": "^5.2.0", + "nlm": "^3.3.1", "prettier": "^1.6.1" }, "author": { diff --git a/test/timeout.test.js b/test/timeout.test.js index 6c2a221..21e2a01 100644 --- a/test/timeout.test.js +++ b/test/timeout.test.js @@ -1,6 +1,6 @@ import assert from 'assertive'; import Bluebird from 'bluebird'; -import { identity } from 'lodash'; +const identity = val => val; import Cache from '../lib/cache'; From 23dcc6aa409f82be5485f3041948a9cdd1c1755e Mon Sep 17 00:00:00 2001 From: Andreas Richter Date: Thu, 13 Sep 2018 20:16:48 -0500 Subject: [PATCH 2/2] chore: feedback addressed --- lib/backends/memcached.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/backends/memcached.js b/lib/backends/memcached.js index e12ca9c..c30ed83 100644 --- a/lib/backends/memcached.js +++ b/lib/backends/memcached.js @@ -35,6 +35,8 @@ const promisify = require('bluebird').promisify; const Memcached = require('memcached'); +const noop = () => undefined; + function createClient(options) { if (options.client) { return options.client; @@ -62,11 +64,12 @@ MemcachedBackend.prototype.get = function get(key) { }; MemcachedBackend.prototype.set = function set(key, value, options) { - return this._clientSet(key, value, options.expire).then(val => val); + const constant = () => value; + return this._clientSet(key, value, options.expire).then(constant); }; MemcachedBackend.prototype.unset = function unset(key) { - return this._clientDel(key).then(() => undefined); + return this._clientDel(key).then(noop); }; MemcachedBackend.prototype.end = function end() {