Skip to content

Instantly share code, notes, and snippets.

@faustoct1
Created June 26, 2022 14:32
Show Gist options
  • Save faustoct1/f71779367ff0658a4c24ae7c631db127 to your computer and use it in GitHub Desktop.
Save faustoct1/f71779367ff0658a4c24ae7c631db127 to your computer and use it in GitHub Desktop.
Usando redis em ambientes serverless
/*
1. require redis wrapper para executar open/close da conexão
2. Setar sua conexão redis na URL, use uma ENV do seu servidor
para não armazenar usuário/senha hardcoded.
*/
const {redis} = require('./redis')
redis(async (client)=>{
return Promise.all([
client.set(key, JSON.stringify(values))
])
},URL)
/*
Esse código executa uma sessão do redis em ambientes serverless / stateless.
redis
*/
const {createClient} = require('redis')
exports.redis = async (callback,url) => {
let client = null
try{
client = createClient({ url: url })
await client.connect()
const call = async () => {
try{
return await callback(client)
}catch(e){
console.log(e)
return null
}finally{
if(client)client.quit()
}
}
return call()
}catch(e){
console.log(e)
return null
}finally{
//if(client)client.quit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment