Skip to content

Commit

Permalink
Merge pull request #38 from groupon/apply-nlm-generator
Browse files Browse the repository at this point in the history
Apply latest nlm generator
  • Loading branch information
markowsiak authored Jan 5, 2018
2 parents 412a0fa + 35b216e commit 4984522
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "groupon/legacy"
"extends": "groupon/node4"
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/yarn.lock
/package-lock.json
node_modules/
npm-debug.log
/tmp
Expand Down
22 changes: 9 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
language: node_js
node_js:
- '0.10'
- '4'
before_install:
- npm install -g npm@latest-2
before_deploy:
- 'git config --global user.email "[email protected]"'
- 'git config --global user.name "Groupon"'
- 4.6.1
- 6.11.5
- 8.9.0
deploy:
provider: script
script: ./node_modules/.bin/nlm release
skip_cleanup: true
'on':
branch: master
node: '4'
- provider: script
script: ./node_modules/.bin/nlm release
skip_cleanup: true
'on':
branch: master
node: 8.9.0
services: memcached
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Generated by generator-nlm -->
<!-- Generated by generator-js -->

# Contributing

Expand All @@ -8,6 +8,7 @@ This document outlines some of the practices we care about.
If you have any questions or suggestions about the process,
feel free to [open an issue](#reporting-issues)
.

## How Can I Contribute?

### Reporting Issues
Expand Down Expand Up @@ -51,7 +52,7 @@ The general steps for creating a pull request are:
1. If you're fixing a bug, be sure to write a test *first*.
That way you can validate that the test actually catches the bug and doesn't pass.
1. Make your changes to the code.
Remember to update the tests if you add new features or change behavior.
Remember to update the tests if you add new features or change behavior.
1. Run the tests via `npm test`. This will also run style checks and other validations.
You might see errors about uncommitted files.
This is expected until you commit your changes.
Expand Down
9 changes: 5 additions & 4 deletions lib/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

'use strict';

var typeMap = Object.create(null);
const typeMap = Object.create(null);

function isBackend(object) {
return typeof object.get === 'function' && typeof object.set === 'function';
Expand All @@ -44,10 +45,10 @@ exports.create = function create(options) {
return options;
}

var type = options.type || 'noop';
var BackendClass = typeMap[type];
const type = options.type || 'noop';
const BackendClass = typeMap[type];
if (!BackendClass) {
throw new Error(type + ' is not a supported cache backend type');
throw new Error(`${type} is not a supported cache backend type`);
}
return new BackendClass(options);
};
Expand Down
14 changes: 7 additions & 7 deletions lib/backends/memcached.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

'use strict';

var promisify = require('bluebird').promisify;
var _ = require('lodash');
var Memcached = require('memcached');
const promisify = require('bluebird').promisify;
const _ = require('lodash');
const Memcached = require('memcached');

function createClient(options) {
if (options.client) {
return options.client;
}
var hosts = options.hosts || '127.0.0.1:11211';
const hosts = options.hosts || '127.0.0.1:11211';
return new Memcached(hosts, options);
}

Expand All @@ -50,7 +51,7 @@ function normalizeValue(value) {
/* Uses anything supporting the memcache protocol */
function MemcachedBackend(options) {
this.type = 'memcached';
var client = this.client = createClient(options);
const client = (this.client = createClient(options));
this._clientGet = promisify(client.get, { context: client });
this._clientSet = promisify(client.set, { context: client });
this._clientDel = promisify(client.del, { context: client });
Expand All @@ -62,8 +63,7 @@ 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(_.constant(value));
};

MemcachedBackend.prototype.unset = function unset(key) {
Expand Down
7 changes: 4 additions & 3 deletions lib/backends/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

'use strict';

var Bluebird = require('bluebird');
const Bluebird = require('bluebird');

var util = require('../util');
const util = require('../util');

/* Stores everything just in memory */
function MemoryBackend() {
Expand All @@ -43,7 +44,7 @@ function MemoryBackend() {
module.exports = MemoryBackend;

MemoryBackend.prototype.get = function get(key) {
var wrappedValue = this.cache[key] || null;
let wrappedValue = this.cache[key] || null;
if (util.isExpired(wrappedValue && wrappedValue.e)) {
wrappedValue = null;
delete this.cache[key];
Expand Down
3 changes: 2 additions & 1 deletion lib/backends/noop.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

'use strict';

var Bluebird = require('bluebird');
const Bluebird = require('bluebird');

/* Simple backend doing nothing */
function NoopBackend() {
Expand Down
58 changes: 35 additions & 23 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,24 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* eslint-disable no-underscore-dangle */

'use strict';

var _ = require('lodash');
const _ = require('lodash');

var Backend = require('./backend');
var getOrElse = require('./get-or-else');
var util = require('./util');
const Backend = require('./backend');
const getOrElse = require('./get-or-else');
const util = require('./util');

function Cache(options) {
this.defaults = {
freshFor: 0,
expire: 0,
};
this.name = options.name || 'default';
this.prefix = this.name + ':';
this.prefix = `${this.name}:`;
this.staleOrPending = {};

this.setDefaults(options.defaults);
Expand All @@ -60,9 +63,12 @@ Cache.prototype.setDefaults = function setDefaults(defaults) {
};

Cache.prototype.setBackend = function setBackend(backendOptions) {
backendOptions = typeof backendOptions === 'string' ? {
type: backendOptions,
} : backendOptions || {};
backendOptions =
typeof backendOptions === 'string'
? {
type: backendOptions,
}
: backendOptions || {};
this.end();
this.backend = Backend.create(backendOptions);
return this.backend;
Expand All @@ -79,27 +85,31 @@ Cache.prototype.prepareOptions = function prepareOptions(options) {
};

Cache.prototype._set = function _set(key, val, options) {
var self = this;
const self = this;
function writeToBackend(resolvedValue) {
return self.backend.set(key, {
b: util.expiresAt(options.freshFor),
d: resolvedValue,
}, options);
return self.backend.set(
key,
{
b: util.expiresAt(options.freshFor),
d: resolvedValue,
},
options
);
}

return this._applyTimeout(util.toPromise(val).then(writeToBackend));
};

Cache.prototype.set = function set(rawKey, val, _opts, _cb) {
var args = util.optionalOpts(_opts, _cb);
var key = this.applyPrefix(rawKey);
var optsWithDefaults = this.prepareOptions(args.opts);
const args = util.optionalOpts(_opts, _cb);
const key = this.applyPrefix(rawKey);
const optsWithDefaults = this.prepareOptions(args.opts);

return this._set(key, val, optsWithDefaults).nodeify(args.cb);
};

Cache.prototype._applyTimeout = function _applyTimeout(value) {
var timeoutMs = this.defaults.timeout;
const timeoutMs = this.defaults.timeout;
if (timeoutMs > 0) {
return value.timeout(timeoutMs);
}
Expand All @@ -114,21 +124,23 @@ Cache.prototype._getWrapped = function _getWrapped(key) {
Cache.prototype.getWrapped = Cache.prototype._getWrapped;

Cache.prototype.get = function get(rawKey, cb) {
var key = this.applyPrefix(rawKey);
const key = this.applyPrefix(rawKey);

return this._getWrapped(key).then(util.extractValue).nodeify(cb);
return this._getWrapped(key)
.then(util.extractValue)
.nodeify(cb);
};

Cache.prototype.getOrElse = function _getOrElse(rawKey, val, _opts, _cb) {
var key = this.applyPrefix(rawKey);
var args = util.optionalOpts(_opts, _cb);
var optsWithDefaults = this.prepareOptions(args.opts);
const key = this.applyPrefix(rawKey);
const args = util.optionalOpts(_opts, _cb);
const optsWithDefaults = this.prepareOptions(args.opts);

return getOrElse(this, key, val, optsWithDefaults).nodeify(args.cb);
};

Cache.prototype.unset = function unset(rawKey, cb) {
var key = this.applyPrefix(rawKey);
const key = this.applyPrefix(rawKey);

return this._unset(key).nodeify(cb);
};
Expand Down
24 changes: 15 additions & 9 deletions lib/cached.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

'use strict';

var Bluebird = require('bluebird');
var _ = require('lodash');
var util = require('util');
const Bluebird = require('bluebird');
const _ = require('lodash');
const util = require('util');

var Cache = require('./cache');
const Cache = require('./cache');

var namedCaches = Object.create(null);
let namedCaches = Object.create(null);

var getName = util.deprecate(function getName(name) {
const getName = util.deprecate(function getName(name) {
return name || 'default';
}, 'Unnamed caches and caches with non-string names are deprecated.');

Expand All @@ -49,9 +50,14 @@ function cached(name, options) {
}

if (!(name in namedCaches)) {
namedCaches[name] = cached.createCache(_.extend({
name: name,
}, options || {}));
namedCaches[name] = cached.createCache(
_.extend(
{
name: name,
},
options || {}
)
);
}
return namedCaches[name];
}
Expand Down
Loading

0 comments on commit 4984522

Please sign in to comment.