Browse Source

adding JSCS

feature/nested-cache-fetch-fix
Bryan Donovan 10 years ago
parent
commit
7d01586706
  1. 80
      .jscs.json
  2. 3
      Makefile
  3. 6
      examples/example.js
  4. 3
      examples/redis_example/example.js
  5. 10
      lib/caching.js
  6. 25
      lib/multi_caching.js
  7. 3
      package.json
  8. 7
      test/caching.unit.js
  9. 2
      test/multi_caching.unit.js

80
.jscs.json

@ -0,0 +1,80 @@
{
"requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"],
"requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"],
"requireSpaceBeforeBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"requireSpaceAfterBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
"disallowSpaceAfterBinaryOperators": ["!"],
"disallowSpaceBeforeBinaryOperators": [","],
"disallowMultipleVarDecl": true,
"disallowEmptyBlocks": true,
"disallowKeywords": ["with"],
"disallowKeywordsOnNewLine": ["else"],
"disallowSpacesInsideObjectBrackets": true,
"disallowSpacesInsideArrayBrackets": true,
"disallowSpacesInsideParentheses": true,
"disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
"disallowMultipleLineStrings": true,
"disallowTrailingWhitespace": true,
"disallowPaddingNewlinesInBlocks": true,
"requireCommaBeforeLineBreak": true,
"requireParenthesesAroundIIFE": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpacesInConditionalExpression": true,
"requireSpacesInFunctionExpression": {
"beforeOpeningCurlyBrace": true
},
"requireSpaceBeforeBinaryOperators": [
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!=="
],
"requireOperatorBeforeLineBreak": [
"?",
"+",
"-",
"/",
"*",
"=",
"==",
"===",
"!=",
"!==",
">",
">=",
"<",
"<="
],
"safeContextKeyword": "self",
"validateIndentation": 4,
"validateParameterSeparator": ", ",
"excludeFiles": [
"test/data/**",
"test/support/fakes/**"
],
"validateJSDoc": {
"checkParamNames": true,
"requireParamTypes": true
},
"disallowMultipleLineBreaks": true,
"validateLineBreaks": "LF",
"disallowYodaConditions": true
}

3
Makefile

@ -21,6 +21,9 @@ test-travis: lint
./node_modules/.bin/istanbul cover test/run.js --report lcovonly \
-- -T unit,functional -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
lint:
./node_modules/.bin/jscs ./lib && \
./node_modules/.bin/jscs ./test && \
./node_modules/.bin/jscs ./examples && \
./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc
./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc

6
examples/example.js

@ -1,7 +1,8 @@
/*jshint unused:false*/
// Note: ttls are in seconds
var cache_manager = require('../');
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/});
var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100/*seconds*/});
var memory_cache = cache_manager.caching({store: 'memory', max: 100, ttl: 10});
var memory_cache2 = cache_manager.caching({store: 'memory', max: 100, ttl: 100});
var ttl; //Can't use a different ttl per set() call with memory cache
//
@ -74,7 +75,6 @@ get_cached_user(user_id, function (err, user) {
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
// Same as above, but written differently:
memory_cache.wrap(key, function (cb) {
get_user(user_id, cb);

3
examples/redis_example/example.js

@ -5,7 +5,8 @@
var util = require('util');
var cache_manager = require('../../');
var redis_store = require('./redis_store');
var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100/*seconds*/});
// Note: ttl is in seconds
var redis_cache = cache_manager.caching({store: redis_store, db: 0, ttl: 100});
var ttl = 60;
console.log("set/get/del example:");

10
lib/caching.js

@ -35,10 +35,9 @@ var caching = function (args) {
* });
*/
self.wrap = function (key, work, ttl, cb) {
if(typeof(ttl) == 'function') {
cb = ttl;
ttl = undefined;
if (typeof ttl === 'function') {
cb = ttl;
ttl = undefined;
}
self.store.get(key, function (err, result) {
@ -66,8 +65,7 @@ var caching = function (args) {
self.queues[key].forEach(function (done) {
done.call(null, err);
});
}
else {
} else {
self.queues[key].forEach(function (done) {
done.apply(null, work_args);
});

25
lib/multi_caching.js

@ -42,10 +42,9 @@ var multi_caching = function (caches) {
* cache, it gets set in all higher-priority caches.
*/
self.wrap = function (key, work, ttl, cb) {
if(typeof(ttl) == 'function') {
cb = ttl;
ttl = undefined;
if (typeof ttl === 'function') {
cb = ttl;
ttl = undefined;
}
get_from_highest_priority_cache(key, function (err, result, index) {
@ -54,9 +53,9 @@ var multi_caching = function (caches) {
} else if (result) {
var caches_to_update = caches.slice(0, index);
var opts = {
key: key,
value: result,
ttl: ttl
key: key,
value: result,
ttl: ttl
};
set_in_multiple_caches(caches_to_update, opts, function (err) {
cb(err, result);
@ -75,9 +74,9 @@ var multi_caching = function (caches) {
return;
}
var opts = {
key: key,
value: work_args[1],
ttl: ttl
key: key,
value: work_args[1],
ttl: ttl
};
set_in_multiple_caches(caches, opts, function (err) {
if (err) {
@ -99,9 +98,9 @@ var multi_caching = function (caches) {
self.set = function (key, value, ttl, cb) {
var opts = {
key: key,
value: value,
ttl: ttl
key: key,
value: value,
ttl: ttl
};
set_in_multiple_caches(caches, opts, cb);
};

3
package.json

@ -26,7 +26,8 @@
"devDependencies": {
"coveralls": "^2.3.0",
"istanbul": "^0.2.11",
"jshint": "^2.5.1",
"jscs": "^1.7.1",
"jshint": "^2.5.4",
"mocha": "^1.20.1",
"optimist": "^0.6.1",
"sinon": "^1.10.2"

7
test/caching.unit.js

@ -251,7 +251,6 @@ describe("caching", function () {
});
context("calls back with the result of the wrapped function", function () {
beforeEach(function () {
sinon.spy(memory_store_stub, 'set');
});
@ -260,7 +259,7 @@ describe("caching", function () {
memory_store_stub.set.restore();
});
it("when a ttl is passed in", function(done) {
it("when a ttl is passed in", function (done) {
cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, ttl, function (err, widget) {
@ -269,10 +268,9 @@ describe("caching", function () {
sinon.assert.calledWith(memory_store_stub.set, key, {name: name}, ttl);
done();
});
});
it("when a ttl is not passed in", function(done) {
it("when a ttl is not passed in", function (done) {
cache.wrap(key, function (cb) {
methods.get_widget(name, cb);
}, function (err, widget) {
@ -282,7 +280,6 @@ describe("caching", function () {
done();
});
});
});
context("when result is already cached", function () {

2
test/multi_caching.unit.js

@ -185,7 +185,6 @@ describe("multi_caching", function () {
});
context("calls back with the result of a function", function () {
beforeEach(function () {
sinon.spy(memory_cache3.store, 'set');
});
@ -215,7 +214,6 @@ describe("multi_caching", function () {
done();
});
});
});
context("when wrapped function calls back with an error", function () {

Loading…
Cancel
Save