From e3d4007f31dbd5c558222ac6d24d2b13e97aca6d Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Fri, 18 Aug 2023 20:07:10 -0600 Subject: [PATCH] Add caching CNAM --- index.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index e7159d9..25e7d34 100644 --- a/index.js +++ b/index.js @@ -13,20 +13,27 @@ const fs = require('fs'); const app = express(); app.use(express.json()); +cnamCache = {}; + app.post('/sms', async (req, res) => { data = req.body; // Get the CNAM record for the number - cnam = data.From; - await axios.get(`https://cnam.bulkvs.com/?id=${config.bulkvs.id}&did=${data.From}&format=json`, { - headers: { - 'Content-Type': 'application/json' - } - }).then(function (response) { - cnam = response.data.name; + if (cnamCache[data.From]) { + cnam = cnamCache[data.From]; + } else { + cnam = data.From; + await axios.get(`https://cnam.bulkvs.com/?id=${config.bulkvs.id}&did=${data.From}&format=json`, { + headers: { + 'Content-Type': 'application/json' + } + }).then(function (response) { + cnam = response.data.name; - }).catch(function (error) { - console.log(error); - }); + }).catch(function (error) { + console.log(error); + }); + cnamCache[data.From] = cnam; + } if (data.MediaURLs) { // It's an MMS out = { "text": "",