Skip to content

Commit

Permalink
Created the localCache and added some unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
supernomad committed Jun 19, 2015
1 parent 24137ab commit 666ac16
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
45 changes: 45 additions & 0 deletions libs/caching/localCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var NodeCache = require('node-cache'),
cache = new NodeCache({ stdTTL: 0, checkperiod: 300, useClones: false }),
typeHelper = require('./../helpers/typeHelper');

function create(key, val, ttl, callback) {
if(typeHelper.isFunction(callback)) {
cache.set(key, val, ttl, function (error, success) {
callback(error, success);
});
} else {
return cache.set(key, val, ttl);
}
}

function restore(key, callback) {
if(typeHelper.isFunction(callback)) {
cache.get(key, function (error, value) {
callback(error, {key: key, value: value});
});
} else {
var value = cache.get(key);
return {key: key, value: value};
}
}

function update(key, val, ttl, callback) {
return create(key, val, ttl, callback);
}

function del(key, callback) {
if(typeHelper.isFunction(callback)) {
cache.del(key, function (error, count) {
callback(error, count);
});
} else {
return cache.del(key);
}
}

module.exports = {
"create": create,
"restore": restore,
"update": update,
"delete": del
};
112 changes: 112 additions & 0 deletions test/localCache-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* global describe, it */
var should = require('should'),
localCache = require('./../libs/caching/localCache');

describe('localCache.js', function () {
describe('#create', function () {
it('should create and object in the local cache.', function(done) {
var object = { test: "object", testing: 0 };
var key = "test-key-create";
localCache.create(key, object, 30, function (error, success) {
should.not.exist(error);
should.exist(success);
success.should.be.true;
done();
});
});

it('should return true/false if called without a callback', function() {
var object = { test: "object", testing: 0 };
var key = "test-key-create";
var test = localCache.create(key, object, 30);
should.exist(test);
test.should.be.true;
});
});

describe('#restore', function() {
it('should restore the cache item with the specified key', function(done) {
var object = { test: "object", testing: 0 };
var key = "test-key-create";
localCache.restore(key, function (error, keyVal) {
should.not.exist(error);
should.exist(keyVal);
keyVal.key.should.equal(key);
for (var hashKey in keyVal.value) {
keyVal.value[hashKey].should.equal(object[hashKey]);
}
done();
});
});

it('should return the key/value pair if called without a callback', function() {
var object = { test: "object", testing: 0 };
var key = "test-key-create";
var test = localCache.restore(key);

should.exist(test);
test.key.should.equal(key);

for (var hashKey in test.value) {
test.value[hashKey].should.equal(object[hashKey]);
}
});
});

describe('#update', function() {
it('should update the cache item with the new value using the specified key', function(done) {
var object = { test: "object", testing: 2 };
var key = "test-key-create";
localCache.update(key, object, 30, function (error, success) {
should.not.exist(error);
should.exist(success);
success.should.be.true;
localCache.restore(key, function (error, keyVal) {
should.not.exist(error);
should.exist(keyVal);
keyVal.key.should.equal(key);
for (var hashKey in keyVal.value) {
keyVal.value[hashKey].should.equal(object[hashKey]);
}
done();
});
});
});

it('should return true/false if called without a callback', function() {
var object = { test: "object", testing: 0 };
var key = "test-key-create";
var test = localCache.update(key, object, 30);
should.exist(test);
test.should.be.true;

test = localCache.restore(key);

should.exist(test);
test.key.should.equal(key);

for (var hashKey in test.value) {
test.value[hashKey].should.equal(object[hashKey]);
}
});
});

describe('#delete', function() {
it('should delete the cache item with the specified key', function(done) {
var key = "test-key-create";
localCache.delete(key, function (error, count) {
should.not.exist(error);
should.exist(count);
count.should.equal(1);
done();
});
});

it('should return the number of cache items deleted if called without a callback', function() {
var key = "test-key-create";
var test = localCache.delete(key);
should.exist(test);
test.should.equal(0);
});
});
});

0 comments on commit 666ac16

Please sign in to comment.