Created
August 30, 2019 01:50
-
-
Save vanessasoutoc/37a57a14222b2cf424ccd409d01ff51f to your computer and use it in GitHub Desktop.
export const with promise
This file contains hidden or 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
var redis = require('redis') | |
var Promise = require('promise') | |
const models = require('../../models') | |
const configAWS = async () => { | |
await models.configs.obterConfig() | |
} | |
var client = redis.createClient( | |
configAWS.REDIS_PORT, | |
configAWS.REDIS_HOST | |
) | |
const timeInSecond = 'EX' | |
client.on('error', function (err) { | |
console.log('Error ' + err) | |
}) | |
/* | |
client.set('selic', 0.19, timeInSecond, 3600) | |
client.set('ipca', 6, timeInSecond, 3600) | |
client.hset('hash key', 'hashtest 1', 'some value', redis.print) | |
client.hset(['hash key', 'hashtest 2', 'some other value'], redis.print) | |
client.hkeys('hash key', function (err, replies) { | |
console.log(replies.length + ' replies:') | |
replies.forEach(function (reply, i) { | |
console.log(' ' + i + ': ' + reply) | |
}) | |
if (err) { | |
console.log(err) | |
} | |
client.quit() | |
}) | |
*/ | |
module.exports = { | |
setCache: (data) => { | |
var promise = new Promise((resolve, reject) => { | |
client.set(data.key, data.value, timeInSecond, data.time, function (err, res) { | |
if (err) { | |
return reject(err) | |
} | |
return resolve(res) | |
}) | |
}) | |
return promise | |
}, | |
getCache: (key) => { | |
var promise = new Promise((resolve, reject) => { | |
client.get(key, function (err, reply) { | |
if (err) { | |
return reject(err) | |
} | |
return resolve(reply) | |
}) | |
}) | |
return promise | |
}, | |
delCache: (key) => { | |
var promise = new Promise((resolve, reject) => { | |
client.del(key, function (err, reply) { | |
if (err) { | |
return reject(err) | |
} | |
return resolve(reply) | |
}) | |
}) | |
return promise | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment