Skip to content

Commit

Permalink
Migrating all possible loops to Promise.all
Browse files Browse the repository at this point in the history
  • Loading branch information
pdellaert committed Oct 27, 2024
1 parent afa2a93 commit cf72e94
Showing 1 changed file with 10 additions and 41 deletions.
51 changes: 10 additions & 41 deletions src/lib/cache/cacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export async function clearSinglePrefixCommandCache(command: IPrefixCommand) {

const { name, aliases } = command;
Logger.debug(`Clearing cache for command or alias "${name}"`);
for (const alias of aliases) {
// eslint-disable-next-line no-await-in-loop
await inMemoryCache.del(`${MemoryCachePrefix.COMMAND}:${alias.toLowerCase()}`);
}
await Promise.all(aliases.map((alias) => inMemoryCache.del(`${MemoryCachePrefix.COMMAND}:${alias.toLowerCase()}`)));
await inMemoryCache.del(`${MemoryCachePrefix.COMMAND}:${name.toLowerCase()}`);
}

Expand All @@ -68,10 +65,7 @@ export async function loadSinglePrefixCommandToCache(command: IPrefixCommand) {
const { name, aliases } = command;
Logger.debug(`Loading command ${name} to cache`);
await inMemoryCache.set(`${MemoryCachePrefix.COMMAND}:${name.toLowerCase()}`, command.toObject());
for (const alias of aliases) {
// eslint-disable-next-line no-await-in-loop
await inMemoryCache.set(`${MemoryCachePrefix.COMMAND}:${alias.toLowerCase()}`, command.toObject());
}
await Promise.all(aliases.map((alias) => inMemoryCache.set(`${MemoryCachePrefix.COMMAND}:${alias.toLowerCase()}`, command.toObject())));
}

export async function loadAllPrefixCommandsToCache() {
Expand All @@ -80,10 +74,7 @@ export async function loadAllPrefixCommandsToCache() {
if (!conn || !inMemoryCache) return;

const prefixCommands = await PrefixCommand.find();
for (const command of prefixCommands) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandToCache(command);
}
await Promise.all(prefixCommands.map((command) => loadSinglePrefixCommandToCache(command)));
}

export async function refreshSinglePrefixCommandCache(oldCommand: IPrefixCommand, newCommand: IPrefixCommand) {
Expand Down Expand Up @@ -122,10 +113,7 @@ export async function refreshAllPrefixCommandsCache() {
}
}
// Step 4: Loop over database commands and update cache
for (const dbCommand of prefixCommands) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandToCache(dbCommand);
}
await Promise.all(prefixCommands.map((dbCommand) => loadSinglePrefixCommandToCache(dbCommand)));
}

/**
Expand Down Expand Up @@ -158,10 +146,7 @@ export async function loadAllPrefixCommandVersionsToCache() {
if (!conn || !inMemoryCache) return;

const prefixCommandVersions = await PrefixCommandVersion.find();
for (const version of prefixCommandVersions) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandVersionToCache(version);
}
await Promise.all(prefixCommandVersions.map((version) => loadSinglePrefixCommandVersionToCache(version)));
}

export async function refreshSinglePrefixCommandVersionCache(oldVersion: IPrefixCommandVersion, newVersion: IPrefixCommandVersion) {
Expand Down Expand Up @@ -200,10 +185,7 @@ export async function refreshAllPrefixCommandVersionsCache() {
}
}
// Step 4: Loop over database versions and update cache
for (const dbVersion of prefixCommandVersions) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandVersionToCache(dbVersion);
}
await Promise.all(prefixCommandVersions.map((dbVersion) => loadSinglePrefixCommandVersionToCache(dbVersion)));
}

/**
Expand Down Expand Up @@ -234,10 +216,7 @@ export async function loadAllPrefixCommandCategoriesToCache() {
if (!conn || !inMemoryCache) return;

const prefixCommandCategories = await PrefixCommandCategory.find();
for (const category of prefixCommandCategories) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandCategoryToCache(category);
}
await Promise.all(prefixCommandCategories.map((category) => loadSinglePrefixCommandCategoryToCache(category)));
}

export async function refreshSinglePrefixCommandCategoryCache(oldCategory: IPrefixCommandCategory, newCategory: IPrefixCommandCategory) {
Expand Down Expand Up @@ -276,10 +255,7 @@ export async function refreshAllPrefixCommandCategoriesCache() {
}
}
// Step 4: Loop over database categories and update cache
for (const dbCategory of prefixCommandCategories) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandCategoryToCache(dbCategory);
}
await Promise.all(prefixCommandCategories.map((dbCategory) => loadSinglePrefixCommandCategoryToCache(dbCategory)));
}

/**
Expand Down Expand Up @@ -313,11 +289,7 @@ export async function loadAllPrefixCommandChannelDefaultVersionsToCache() {
if (!conn || !inMemoryCache) return;

const PrefixCommandChannelDefaultVersions = await PrefixCommandChannelDefaultVersion.find();

for (const defaultVersion of PrefixCommandChannelDefaultVersions) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandChannelDefaultVersionToCache(defaultVersion);
}
await Promise.all(PrefixCommandChannelDefaultVersions.map((channelDefaultVersion) => loadSinglePrefixCommandChannelDefaultVersionToCache(channelDefaultVersion)));
}

export async function refreshAllPrefixCommandChannelDefaultVersionsCache() {
Expand Down Expand Up @@ -351,8 +323,5 @@ export async function refreshAllPrefixCommandChannelDefaultVersionsCache() {
}
}
// Step 4: Loop over database channel default versions and update cache
for (const dbChannelDefaultVersion of prefixCommandChannelDefaultVersions) {
// eslint-disable-next-line no-await-in-loop
await loadSinglePrefixCommandChannelDefaultVersionToCache(dbChannelDefaultVersion);
}
await Promise.all(prefixCommandChannelDefaultVersions.map((dbChannelDefaultVersion) => loadSinglePrefixCommandChannelDefaultVersionToCache(dbChannelDefaultVersion)));
}

0 comments on commit cf72e94

Please sign in to comment.