Skip to content

Commit

Permalink
0.0.1 add util methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanCannon committed Jul 29, 2015
1 parent 95ce480 commit 738e240
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage
.idea
.DS_Store
39 changes: 39 additions & 0 deletions .jshintrc
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
}
}
5 changes: 5 additions & 0 deletions .travis.yml
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"
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
# 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 redis = require('redis'),
redisClient = redis.createClient(),
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 redis = require('redis'),
redisClient = redis.createClient(),
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 redis = require('redis'),
redisClient = redis.createClient(),
cacheUtils = require('alien-node-redis-utils');

cacheUtils.deleteItem(redisClient, 'someKey')
.then(function() {
// cool
})
.catch(function(err) {
// some err from redisClient
});

```
7 changes: 7 additions & 0 deletions lib/Cache.js
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')
};
30 changes: 30 additions & 0 deletions lib/methods/deleteItem.js
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;
30 changes: 30 additions & 0 deletions lib/methods/getItem.js
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;
46 changes: 46 additions & 0 deletions lib/methods/setItem.js
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;
37 changes: 37 additions & 0 deletions package.json
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"
}
32 changes: 32 additions & 0 deletions spec/deleteItemSpec.js
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'));
});

});
32 changes: 32 additions & 0 deletions spec/getItemSpec.js
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'));
});

});
11 changes: 11 additions & 0 deletions spec/helpers/_constants.js
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;
30 changes: 30 additions & 0 deletions spec/helpers/_mockRedisClient.js
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;

0 comments on commit 738e240

Please sign in to comment.