Fix perf with cache

This commit is contained in:
2025-10-10 03:42:57 +07:00
parent bdda24cb60
commit b6b11be33a
13 changed files with 1646 additions and 32 deletions

View File

@@ -199,10 +199,11 @@ public class AccountService : IAccountService
private async Task<IEnumerable<Account>> GetAccountsAsync(User user, bool hideSecrets, bool getBalance)
{
var result = await _accountRepository.GetAccountsAsync();
// Use the new efficient repository method that queries accounts by user directly
var result = await _accountRepository.GetAccountsByUserAsync(user);
var accounts = new List<Account>();
foreach (var account in result.Where(a => a.User.Name == user.Name))
foreach (var account in result)
{
await ManagePropertiesAsync(hideSecrets, getBalance, account);
accounts.Add(account);

View File

@@ -144,12 +144,8 @@ public class UserService : IUserService
// Fetch from database (either cache miss or cache disabled)
var account = await _accountService.GetAccountByKey(address, true, false);
var user = await _userRepository.GetUserByNameAsync(account.User.Name);
var user = await _userRepository.GetUserByNameAsync(account.User.Name, true);
// Use proper async version to avoid DbContext concurrency issues
user.Accounts = (await _accountService.GetAccountsByUserAsync(user)).ToList();
// Save to cache for 5 minutes if caching is enabled (JWT middleware calls this on every request)
if (useCache)
{
_cacheService.SaveValue(cacheKey, user, TimeSpan.FromMinutes(5));