@aarconada/urserver
Version:
Basic Server definitions to develope REST API with a node + express Server
59 lines (53 loc) • 1.88 kB
JavaScript
/**
* Created by ubuntu on 16/08/18.
*/
;
const _ = require('lodash');
const promise = require('bluebird');
const redis = require('redis');
const server = require('./server')();
var client;
function openRedisConnection() {
if(client && client.connected) return client;
if(client) client.end();
client = redis.createClient(server.configuration.redis.connection.port, server.configuration.redis.connection.host, server.configuration.redis.connection.options);
client.on('error', (err) => {
server.debug('REDIS ERROR', err);
server.utils.throwError(server.defaultResponses.redis_connection_error);
});
client.on('connection', () => {
server.debug('REDIS CONNECTED');
});
//if(server.configuration.redis.allowConfig === true) {
// client.config("SET", "notify-keyspace-events", "Ex");
//}
return client;
}
function getExpiryTime(key) {
return server.configuration.redis.expiration;
}
//*** Get Key-Value pair ***/
module.exports.getRedisValue = function(key){
return new promise((resolve, reject) => {
openRedisConnection().get(key, function(err, value) {
resolve([value], err);
});
});
};
//*** Set Key-Value pair ***/
module.exports.setRedisValue = function(key, value) {
return new promise((resolve, reject) => {
openRedisConnection().set(key, value, function(err, value) {
if(!_.isUndefined(err) && !_.isNull(err)) reject(err);
else resolve(value);
});
});
};
module.exports.removeRedisValue = function(key) {
return new promise((resolve, reject) => {
openRedisConnection().del(key, function(err, value) {
if(!_.isUndefined(err) && !_.isNull(err)) reject(err);
else resolve(value);
});
});
};