Skip to content

Commit

Permalink
Support Mongo usage to horizontal scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
fmiras committed Mar 15, 2018
1 parent 6b24dd0 commit deae8a9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 21 deletions.
47 changes: 43 additions & 4 deletions lib/index.js
@@ -1,12 +1,51 @@
const { MongoClient } = require('mongodb')

const responses = []
const { MONGO_URL, MONGO_DB } = process.env

const retrieveDataMongo = async url => {
const connection = await MongoClient.connect(MONGO_URL)
const db = connection.db(MONGO_DB)
const collection = db.collection('responses')
const lastCall = await collection.findOne({ url })
connection.close()
return lastCall
}

const retrieveData = async url => {
if (MONGO_URL && MONGO_DB) {
return retrieveDataMongo(url)
}

return responses[url]
}

const saveDataMongo = async (url, { data, date }) => {
const connection = await MongoClient.connect(MONGO_URL)
const db = connection.db(MONGO_DB)
const collection = await db.collection('responses')
await collection.remove({ url })
const lastCall = await collection.insert({ url, data, date })
connection.close()
return lastCall
}

const saveData = async (url, data) => {
if (MONGO_URL && MONGO_DB) {
return saveDataMongo(url, data)
}

responses[url] = data
return data
}

module.exports = (ms, microFunction) => (req, res) => {
const lastCall = responses[req.url]
module.exports = (ms, microFunction) => async (req, res) => {
const lastCall = await retrieveData(req.url)
if (lastCall && lastCall.date > new Date()) {
return lastCall.data
}
const data = microFunction(req, res)
const data = await microFunction(req, res)
const date = new Date(new Date().getTime() + ms)
responses[req.url] = { data, date }
saveData(req.url, { data, date })
return data
}
57 changes: 47 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -41,5 +41,8 @@
"sinon": "4.4.6",
"sleep": "5.1.1",
"xo": "0.20.3"
},
"dependencies": {
"mongodb": "3.0.4"
}
}
14 changes: 7 additions & 7 deletions test/index.js
Expand Up @@ -9,23 +9,23 @@ test('cache(...args) execute the handler twice with 3 requests', async t => {
handler.onCall(0).returns(1)
handler.onCall(1).returns(2)

const microFn = cache(1000, handler)
const microFn = await cache(1000, handler)
const req = { url: 'https://someapi.com' }

t.is(1, microFn(req))
t.is(1, microFn(req))
t.is(1, await microFn(req))
t.is(1, await microFn(req))
await sleep(1)
t.is(2, microFn(req))
t.is(2, await microFn(req))
t.is(2, handler.callCount)
})

test('cache(...args) execute the handler if different url', t => {
test('cache(...args) execute the handler if different url', async t => {
const handler = sinon.stub()
handler.onCall(0).returns(1)
handler.onCall(1).returns(2)

const microFn = cache(1000, handler)

t.is(1, microFn({ url: 'https://someapi.com/resource/1' }))
t.is(2, microFn({ url: 'https://someapi.com/resource/2' }))
t.is(1, await microFn({ url: 'https://someapi.com/resource/1' }))
t.is(2, await microFn({ url: 'https://someapi.com/resource/2' }))
})

0 comments on commit deae8a9

Please sign in to comment.