Skip to content

Commit

Permalink
perf: prevent duplicate cache refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Jun 30, 2022
1 parent e8802bb commit 6cfee2f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"start-s": "tsx src/server.ts"
},
"dependencies": {
"await-lock": "^2.2.2",
"axios": "^0.27.2",
"axios-retry": "^3.2.5",
"cheerio": "^1.0.0-rc.11",
Expand Down
32 changes: 24 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Command } from "commander";
import { bin, version } from "../package.json";
import _ from "lodash";
import { readFileSync } from "fs";
import AwaitLock from "await-lock";

const app = express();

Expand Down Expand Up @@ -84,6 +85,8 @@ const contestsCache: {
lastUpdate: 0
};

const lock = new AwaitLock();

function icsPostProcess(value: string)
{
return value
Expand All @@ -104,17 +107,30 @@ function icsPostProcess(value: string)
);
}

async function getIcs(lang: string, ojs: string[])
async function checkCache()
{
if(contestsCache.lastUpdate < Date.now() - 1000 * 60 * 5)
await lock.acquireAsync();
try
{
if(contestsCache.lastUpdate < Date.now() - 1000 * 60 * 5)
{
const c = await new Lscontests({ days: -1 }).getContests();
contestsCache.contests = _.uniqBy(contestsCache.contests.concat(c.running.concat(c.upcoming)), JSON.stringify);
contestsCache.contests = contestsCache.contests.filter(
c => c.endTime.getTime() - c.startTime.getTime() < 1000 * 60 * 60 * 24 * 2
); // ignore too long contests
contestsCache.lastUpdate = Date.now();
}
}
finally
{
const c = await new Lscontests({ days: -1 }).getContests();
contestsCache.contests = _.uniqBy(contestsCache.contests.concat(c.running.concat(c.upcoming)), JSON.stringify);
contestsCache.contests = contestsCache.contests.filter(
c => c.endTime.getTime() - c.startTime.getTime() < 1000 * 60 * 60 * 24 * 2
); // ignore too long contests
contestsCache.lastUpdate = Date.now();
lock.release();
}
}

async function getIcs(lang: string, ojs: string[])
{
await checkCache();

const contests = contestsCache.contests.filter(c => ojs.some(oj => c.ojName === alloj[oj].name));
const langDict = await getLangDict(lang);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

await-lock@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/await-lock/-/await-lock-2.2.2.tgz#a95a9b269bfd2f69d22b17a321686f551152bcef"
integrity sha512-aDczADvlvTGajTDjcjpJMqRkOF6Qdz3YbPZm/PyW6tKPkx2hlYBzxMhEywM/tU72HrVZjgl5VCdRuMlA7pZ8Gw==

axios-retry@^3.2.5:
version "3.3.1"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.3.1.tgz#47624646138aedefbad2ac32f226f4ee94b6dcab"
Expand Down

0 comments on commit 6cfee2f

Please sign in to comment.