redis-superpowers
v0.2.2
Published
Helper methods for Redis and global connection handler
Downloads
4
Readme
node-redis-promise
Helper methods for Redis and global connection handler
Note: This module uses ES6 code and it's meant to be used with iojs in conjunction with the --es_staging
flag.
Install
npm install node-redis-promise --save
Usage Example
First you need to create a global connection, for example in your index.js
:
var redis = require('node-redis-promise');
redis.connect();
And then in foo.js
:
var redis = require('node-redis-promise');
redis.set({foo: 'bar'}, {
prefix: 'bar_',
expire: 60 // TTL of 1 minute
})
.then(function(key) {
console.log(key); // => bar_sjk21x
return redis.get(key, true); // get key and JSON-decode the value
})
.then(function(bar) {
console.log(bar); // => {foo: 'bar'}
})
.catch(function(err) {
// always handle your errors!
});
If you need to quit the global conneciton you can do so it two ways:
var redis = require('node-redis-promise');
redis.quit();
// OR
global.redisCloseConn();
API
redis.connect()
Create a connection to Redis. Refer to the Node Redis module documentation.
Returns a Promise:
- Fulfilled: on succesfull connection
- Rejected: when a connection error occours
Promise, fulfilled on a succesfull connection and reject on a connection error
redis.get(key [, json])
Get the value of a key in Redis.
Params:
key
(String): key to lookupjson
(Boolean, optional): if set totrue
, JSON-decode the result
Returns: Promise
redis.set(value [, options])
Set a value in Redis, with some advanced options.
Params:
value
: value to storeoptions
(Object, optional): see below avaliable options
Options:
key
(String): key to use when storing the value, defaults to a generated id usingshortid
prefix
(String): prefixeskeys
with the specified valuejson
(Boolean): JSON-encode the value before storing it. By default automatically attempts to detect Objects by usingtypeof value === 'object'
expire
(Number): if sets, it will be used to set the TTL option on the value (seconds).
redis.client
Native Redis client
redis.quit
Safely closes the Redis conneciton
Tests
npm test
Contributing
Yes please. There is a .jshintrc
and .jscsrc
in this repo.
Use your favorite editor JsHint and JSCS plugins to lint the code, please.