Skip to content

Commit

Permalink
infra: use in memory caching to improve endpoint perfromance (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam authored Mar 21, 2019
1 parent e32e442 commit 5050e61
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/serverless-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ async function getInstallations(app) {
)
}

const accountsCache = {}

async function popularInstallations({ app, installations }) {
let popular = await Promise.all(
installations.map(async installation => {
if (accountsCache[installation.id]) {
return accountsCache[installation.id]
}

const { account } = installation

const github = await app.auth(installation.id)
Expand All @@ -42,6 +48,8 @@ async function popularInstallations({ app, installations }) {
return stars + repository.stargazers_count
}, 0)

accountsCache[installation.id] = account

return account
}),
)
Expand All @@ -50,6 +58,8 @@ async function popularInstallations({ app, installations }) {
return popular.sort((a, b) => b.stars - a.stars).slice(0, 10)
}

let installationCache

async function getStats(probot) {
const app = probot.apps[0]

Expand All @@ -59,10 +69,17 @@ async function getStats(probot) {
)
}

const installations = await getInstallations(app)
const popular = await popularInstallations({ app, installations })
if (!installationCache) {
const installationsNew = await getInstallations(app)
installationCache = installationsNew
}

const popular = await popularInstallations({
app,
installations: installationCache,
})
return {
installations: installations.length,
installations: installationCache.length,
popular,
}
}
Expand Down

0 comments on commit 5050e61

Please sign in to comment.