-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the localCache and added some unittests
- Loading branch information
1 parent
24137ab
commit 666ac16
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |