Skip to content

Commit

Permalink
fix: Better performance in batch query
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Dec 3, 2023
1 parent 9e64da2 commit 67fe423
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 1 addition & 10 deletions CFLookup/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,7 @@ public async Task<IActionResult> OnPostAsync()
{
var projectIds = ProjectSearchField.Split(',').Select(p => int.TryParse(p, out var id) ? id : -1).Where(p => p != -1).ToList();

var foundMods = new List<Mod>();

foreach (var id in projectIds)
{
var foundMod = await SharedMethods.SearchModAsync(_redis, _cfApiClient, id);
if (foundMod != null)
{
foundMods.Add(foundMod);
}
}
var foundMods = await SharedMethods.SearchModsAsync(_redis, _cfApiClient, projectIds);

if (foundMods.Count > 0)
{
Expand Down
15 changes: 15 additions & 0 deletions CFLookup/SharedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public static async Task<List<Category>> GetCategoryInfo(IDatabaseAsync _redis,
return categories.Data;
}

public static async Task<List<Mod>> SearchModsAsync(IDatabaseAsync _redis, ApiClient _cfApiClient, List<int> modIds)
{
var cachedMods = await _redis.StringGetAsync($"cf-mods-{string.Join('-', modIds)}");
if (!cachedMods.IsNullOrEmpty)
{
return JsonConvert.DeserializeObject<List<Mod>>(cachedMods);
}

var mods = await _cfApiClient.GetModsByIdListAsync(new GetModsByIdsListRequestBody { ModIds = modIds });

await _redis.StringSetAsync($"cf-mods-{string.Join('-', modIds)}", JsonConvert.SerializeObject(mods.Data), TimeSpan.FromMinutes(5));

return mods.Data;
}

public static async Task<Mod?> SearchModAsync(IDatabaseAsync _redis, ApiClient _cfApiClient, int projectId)
{
var modResultCache = await _redis.StringGetAsync($"cf-mod-{projectId}");
Expand Down

0 comments on commit 67fe423

Please sign in to comment.