diff --git a/api/util/startup/cache.js b/api/util/startup/cache.js index a0ef987..2cea77e 100644 --- a/api/util/startup/cache.js +++ b/api/util/startup/cache.js @@ -4,7 +4,9 @@ const chalk = require('chalk'); module.exports = async app => { app.cache = { - users: {} + users: {}, + series: {}, + seriesCount: 0 }; return new Promise(async resolve => { @@ -14,6 +16,9 @@ module.exports = async app => { let userCache = spin.add("ar", {text: "Caching Users..."}); loaders.push(require('./cache/users')(app, userCache)); + let seriesCache = spin.add("ar", {text: "Caching Series..."}); + loaders.push(require('./cache/series')(app, seriesCache)); + await Promise.all(loaders); console.log(''); resolve(0); diff --git a/api/util/startup/cache/series.js b/api/util/startup/cache/series.js new file mode 100644 index 0000000..69e84c8 --- /dev/null +++ b/api/util/startup/cache/series.js @@ -0,0 +1,27 @@ +const chalk = require('chalk'); + +module.exports = async (app, spinner) => { + const Series = app.db.models.ani.series; + + return new Promise(async resolve => { + const st = new Date().getTime(); + + let amount = 1; + + for await (const series of Series.find()) { + let {id, name, romaji, kanji, altNames, genres, tags} = series; + app.cache.series[series.id] = {id, name, romaji, kanji, altNames, genres, tags}; //keep an in-memory index of series' searchable items + app.cache.series[series.id].synopsis = series.synopsis.synopsis; + console.log(app.cache.series[series.id]); + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`ani DB series.`)}`}); + app.cache.seriesCount++; + amount++; + } + + const cacheTime = new Date().getTime() - st; + spinner.update({text: `${spinner.options.text.slice(0, 19).trim()} ${chalk.gray(`${cacheTime}ms >>`.padStart(8, '0').padStart(7, '0'))} ${spinner.options.text.slice(19).trim()}`}); + spinner.status('non-spinnable'); + + resolve(0); + }); +} \ No newline at end of file