-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95ce480
commit 43c0b8d
Showing
15 changed files
with
430 additions
and
1 deletion.
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,4 @@ | ||
node_modules | ||
coverage | ||
.idea | ||
.DS_Store |
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,39 @@ | ||
{ | ||
"nonew" : true, | ||
"plusplus" : true, | ||
"curly" : true, | ||
"latedef" : "nofunc", | ||
"unused" : "strict", | ||
"noarg" : true, | ||
"indent" : 2, | ||
"forin" : true, | ||
"noempty" : true, | ||
"quotmark" : "single", | ||
"maxparams" : 5, | ||
"node" : true, | ||
"eqeqeq" : true, | ||
"strict" : true, | ||
"undef" : true, | ||
"bitwise" : true, | ||
"newcap" : true, | ||
"immed" : true, | ||
"browser" : true, | ||
"camelcase" : true, | ||
"nonbsp" : true, | ||
"globals" : { | ||
"after" : false, | ||
"afterEach" : false, | ||
"angular" : false, | ||
"before" : false, | ||
"beforeEach" : false, | ||
"browser" : false, | ||
"describe" : false, | ||
"expect" : false, | ||
"inject" : false, | ||
"it" : false, | ||
"jasmine" : false, | ||
"spyOn" : false, | ||
"$" : false, | ||
"_" : false | ||
} | ||
} |
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,5 @@ | ||
language: node_js | ||
node_js: | ||
- "0.12" | ||
after_script: | ||
- "./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info" |
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 |
---|---|---|
@@ -1,2 +1,71 @@ | ||
# alien-node-redis-utils | ||
Helper functions for Redis on NodeJS | ||
Helper functions for Redis cache on NodeJS. The functions are pure and curried with Ramda. | ||
|
||
[![Build Status](https://travis-ci.org/AlienCreations/alien-node-redis-utils.svg?branch=master)](https://travis-ci.org/AlienCreations/alien-node-redis-utils) [![Coverage Status](https://coveralls.io/repos/AlienCreations/alien-node-redis-utils/badge.svg?branch=master&service=github)](https://coveralls.io/github/AlienCreations/alien-node-redis-utils?branch=master) [![npm version](http://img.shields.io/npm/v/alien-node-redis-utils.svg)](https://npmjs.org/package/alien-node-redis-utils) [![Dependency Status](https://david-dm.org/AlienCreations/alien-node-redis-utils.svg)](https://david-dm.org/AlienCreations/alien-node-redis-utils) | ||
|
||
## Install | ||
|
||
``` | ||
$ npm install alien-node-redis-utils --save | ||
``` | ||
|
||
Run the specs | ||
|
||
``` | ||
$ npm test | ||
``` | ||
|
||
## Methods | ||
|
||
#### getItem | ||
Get an item from the Redis store, provided a recognized `cacheKey` | ||
|
||
```js | ||
|
||
var cacheUtils = require('alien-node-redis-utils'); | ||
|
||
cacheUtils.getItem(redisClient, 'someKey') | ||
.then(function(item) { | ||
// cool | ||
}) | ||
.catch(function(err) { | ||
// no item found matching cacheKey | ||
}); | ||
|
||
``` | ||
|
||
#### setItem | ||
Set an item in the Redis store. Adds if key does not exist, otherwise updates the cache. | ||
|
||
```js | ||
|
||
var cacheUtils = require('alien-node-redis-utils'); | ||
|
||
var TWO_HOURS_IN_SECONDS_CACHE_EXPIRE = 1000 * 60 * 60 * 2; | ||
|
||
var cacheKey = 'someKey', | ||
data = { foo : 'bar' }; | ||
|
||
cacheUtils.setItem(redisClient, cacheKey, TWO_HOURS_IN_SECONDS_CACHE_EXPIRE, data); | ||
.then(function(data) { | ||
// cool | ||
}); | ||
|
||
``` | ||
|
||
#### deleteItem | ||
Delete an item from the Redis store, provided a recognized `cacheKey` | ||
|
||
```js | ||
|
||
var cacheUtils = require('alien-node-redis-utils'); | ||
|
||
cacheUtils.deleteItem(redisClient, 'someKey') | ||
.then(function() { | ||
// cool | ||
}) | ||
.catch(function(err) { | ||
// some err from redisClient | ||
}); | ||
|
||
``` |
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,7 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
deleteItem : require('./methods/deleteItem.js'), | ||
getItem : require('./methods/deleteItem'), | ||
setItem : require('./methods/setItem') | ||
}; |
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,30 @@ | ||
'use strict'; | ||
|
||
var Q = require('q'), | ||
R = require('ramda'), | ||
Validator = require('o-validator'), | ||
promiseUtils = require('alien-node-q-utils'); | ||
|
||
var isString = R.is(String), | ||
requiredString = Validator.required(isString); | ||
|
||
var validateCacheKey = Validator.validateOrThrow({ | ||
cacheKey : requiredString | ||
}); | ||
|
||
/** | ||
* Delete an item from the redis store | ||
* @param {RedisClient} redisClient | ||
* @param {String} cacheKey | ||
* @returns {Promise} | ||
*/ | ||
var deleteItem = R.curry(function(redisClient, cacheKey) { | ||
var deferred = Q.defer(); | ||
|
||
validateCacheKey({ cacheKey : cacheKey }); | ||
redisClient.del(cacheKey, promiseUtils.rejectOnErrorOrResolve(deferred)); | ||
|
||
return deferred.promise; | ||
}); | ||
|
||
module.exports = deleteItem; |
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,30 @@ | ||
'use strict'; | ||
|
||
var Q = require('q'), | ||
R = require('ramda'), | ||
Validator = require('o-validator'), | ||
promiseUtils = require('alien-node-q-utils'); | ||
|
||
var isString = R.is(String), | ||
requiredString = Validator.required(isString); | ||
|
||
var validateCacheKey = Validator.validateOrThrow({ | ||
cacheKey : requiredString | ||
}); | ||
|
||
/** | ||
* Get an item from the redis store | ||
* @param {RedisClient} redisClient | ||
* @param {String} cacheKey | ||
* @returns {Promise} | ||
*/ | ||
var getItem = R.curry(function(redisClient, cacheKey) { | ||
var deferred = Q.defer(); | ||
|
||
validateCacheKey({ cacheKey : cacheKey }); | ||
redisClient.get(cacheKey, promiseUtils.rejectOnErrorOrResolve(deferred)); | ||
|
||
return deferred.promise; | ||
}); | ||
|
||
module.exports = getItem; |
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,46 @@ | ||
'use strict'; | ||
|
||
var Q = require('q'), | ||
R = require('ramda'), | ||
Validator = require('o-validator'); | ||
|
||
var ONE_HOUR_IN_SECONDS_TTL_DEFAULT = 1000 * 60 * 60; | ||
|
||
var isString = R.is(String), | ||
isNumber = R.is(Number), | ||
isObject = R.is(Object), | ||
requiredString = Validator.required(isString), | ||
requiredNumber = Validator.required(isNumber), | ||
requiredObject = Validator.required(isObject); | ||
|
||
var validateCacheKey = Validator.validateOrThrow({ | ||
cacheKey : requiredString, | ||
cacheExpire : requiredNumber, | ||
data : requiredObject | ||
}); | ||
|
||
/** | ||
* Set an item to the redis store | ||
* @param {RedisClient} redisClient | ||
* @param {String} cacheKey | ||
* @param {Number} cacheExpire TTL in seconds | ||
* @param {Object} data JSON.stringify-able object | ||
* @returns {Promise} | ||
*/ | ||
var setItem = R.curry(function(redisClient, cacheKey, cacheExpire, data) { | ||
var deferred = Q.defer(); | ||
|
||
validateCacheKey({ | ||
cacheKey : cacheKey, | ||
cacheExpire : cacheExpire, | ||
data : data | ||
}); | ||
|
||
redisClient.set(cacheKey, JSON.stringify(data)); | ||
redisClient.expires(cacheKey, cacheExpire || ONE_HOUR_IN_SECONDS_TTL_DEFAULT); | ||
|
||
deferred.resolve(data); | ||
return deferred.promise; | ||
}); | ||
|
||
module.exports = setItem; |
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,37 @@ | ||
{ | ||
"name" : "alien-node-redis-utils", | ||
"version" : "0.0.1", | ||
"description" : "Helper functions for Redis cache on NodeJS", | ||
"main" : "lib/Cache.js", | ||
"dependencies" : { | ||
"ramda" : "^0.17.x", | ||
"q" : "^2.0.x", | ||
"o-validator" : "^0.1.x", | ||
"alien-node-q-utils" : "*" | ||
}, | ||
"devDependencies" : { | ||
"coveralls" : "^2.11.2", | ||
"istanbul" : "^0.3.13", | ||
"jasmine" : "^2.3.1" | ||
}, | ||
"scripts" : { | ||
"test" : "./node_modules/.bin/istanbul cover ./node_modules/.bin/jasmine" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "https://github.com/aliencreations/alien-node-redis-utils.git" | ||
}, | ||
"keywords" : [ | ||
"aliencreations", | ||
"redis", | ||
"util", | ||
"node", | ||
"ramda" | ||
], | ||
"author" : "Sean Cannon", | ||
"license" : "MIT", | ||
"bugs" : { | ||
"url" : "https://github.com/aliencreations/alien-node-redis-utils/issues" | ||
}, | ||
"homepage" : "https://github.com/aliencreations/alien-node-redis-utils" | ||
} |
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,32 @@ | ||
'use strict'; | ||
|
||
var R = require('ramda'), | ||
deleteItem = require('../lib/methods/deleteItem'), | ||
constants = require('./helpers/_constants'), | ||
mockRedisClient = require('./helpers/_mockRedisClient'); | ||
|
||
describe('deleteItem', function() { | ||
|
||
it('should assume a recognized cache key will trigger a success callback from the RedisClient and resolve', function(done) { | ||
deleteItem(mockRedisClient, constants.FAKE_CACHE_KEY_EXISTS) | ||
.then(function(item) { | ||
expect(item).toBe(constants.FAKE_ITEM); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should assume an unrecognized cache key will trigger an error callback from the RedisClient and reject', function(done) { | ||
deleteItem(mockRedisClient, constants.FAKE_CACHE_KEY_NOT_EXISTS) | ||
.catch(function(item) { | ||
expect(item).toBe(constants.FAKE_ERR); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should throw an error when given a cacheKey of type other than String', function() { | ||
expect(function(){ | ||
deleteItem(mockRedisClient, constants.FAKE_CACHE_KEY_INVALID); | ||
}).toThrow(new Error('Illegal value for parameter: cacheKey')); | ||
}); | ||
|
||
}); |
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,32 @@ | ||
'use strict'; | ||
|
||
var R = require('ramda'), | ||
getItem = require('../lib/methods/getItem'), | ||
constants = require('./helpers/_constants'), | ||
mockRedisClient = require('./helpers/_mockRedisClient'); | ||
|
||
describe('getItem', function() { | ||
|
||
it('should assume a recognized cache key will trigger a success callback from the RedisClient and resolve', function(done) { | ||
getItem(mockRedisClient, constants.FAKE_CACHE_KEY_EXISTS) | ||
.then(function(item) { | ||
expect(item).toBe(constants.FAKE_ITEM); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should assume an unrecognized cache key will trigger an error callback from the RedisClient and reject', function(done) { | ||
getItem(mockRedisClient, constants.FAKE_CACHE_KEY_NOT_EXISTS) | ||
.catch(function(item) { | ||
expect(item).toBe(constants.FAKE_ERR); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should throw an error when given a cacheKey of type other than String', function() { | ||
expect(function(){ | ||
getItem(mockRedisClient, constants.FAKE_CACHE_KEY_INVALID); | ||
}).toThrow(new Error('Illegal value for parameter: cacheKey')); | ||
}); | ||
|
||
}); |
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,11 @@ | ||
'use strict'; | ||
|
||
var constants = { | ||
FAKE_CACHE_KEY_EXISTS : 'exists', | ||
FAKE_CACHE_KEY_NOT_EXISTS : 'notexists', | ||
FAKE_CACHE_KEY_INVALID : 123, | ||
FAKE_ITEM : 'someitem', | ||
FAKE_ERR : 'someerr' | ||
}; | ||
|
||
module.exports = constants; |
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,30 @@ | ||
'use strict'; | ||
|
||
var constants = require('./_constants'); | ||
|
||
var _get = function(cacheKey, callback) { | ||
if (cacheKey === constants.FAKE_CACHE_KEY_EXISTS) { | ||
return callback(undefined, constants.FAKE_ITEM); | ||
} else { | ||
return callback(constants.FAKE_ERR, undefined); | ||
} | ||
}; | ||
|
||
var _del = _get; | ||
|
||
var _set = function(cacheKey, stringifiedData) { | ||
return cacheKey; | ||
}; | ||
|
||
var _expires = function(cacheKey, ttl) { | ||
return cacheKey; | ||
}; | ||
|
||
var mockRedisClient = { | ||
'get' : _get, | ||
'del' : _del, | ||
'set' : _set, | ||
'expires' : _expires | ||
}; | ||
|
||
module.exports = mockRedisClient; |
Oops, something went wrong.