Created
June 26, 2022 14:32
-
-
Save faustoct1/f71779367ff0658a4c24ae7c631db127 to your computer and use it in GitHub Desktop.
Usando redis em ambientes serverless
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
/* | |
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) |
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
/* | |
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