Fix perf with cache
This commit is contained in:
1474
src/Managing.Infrastructure.Database/Migrations/20251009202933_AddUserAccountsNavigationProperty.Designer.cs
generated
Normal file
1474
src/Managing.Infrastructure.Database/Migrations/20251009202933_AddUserAccountsNavigationProperty.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Managing.Infrastructure.Databases.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddUserAccountsNavigationProperty : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1292,7 +1292,7 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
modelBuilder.Entity("Managing.Infrastructure.Databases.PostgreSql.Entities.AccountEntity", b =>
|
||||
{
|
||||
b.HasOne("Managing.Infrastructure.Databases.PostgreSql.Entities.UserEntity", "User")
|
||||
.WithMany()
|
||||
.WithMany("Accounts")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.SetNull)
|
||||
.IsRequired();
|
||||
@@ -1460,6 +1460,11 @@ namespace Managing.Infrastructure.Databases.Migrations
|
||||
{
|
||||
b.Navigation("ScenarioIndicators");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Managing.Infrastructure.Databases.PostgreSql.Entities.UserEntity", b =>
|
||||
{
|
||||
b.Navigation("Accounts");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,7 @@ public class UserEntity
|
||||
[MaxLength(255)] public string? AgentName { get; set; }
|
||||
public string? AvatarUrl { get; set; }
|
||||
public string? TelegramChannel { get; set; }
|
||||
|
||||
// Navigation properties
|
||||
public virtual ICollection<AccountEntity> Accounts { get; set; } = new List<AccountEntity>();
|
||||
}
|
||||
@@ -86,7 +86,7 @@ public class ManagingDbContext : DbContext
|
||||
|
||||
// Configure relationship with User
|
||||
entity.HasOne(e => e.User)
|
||||
.WithMany()
|
||||
.WithMany(u => u.Accounts)
|
||||
.HasForeignKey(e => e.UserId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.PostgreSql;
|
||||
@@ -22,6 +23,7 @@ public class PostgreSqlAccountRepository : IAccountRepository
|
||||
{
|
||||
var accountEntity = _context.Accounts
|
||||
.AsTracking() // Explicitly enable tracking for delete operations
|
||||
.Include(a => a.User) // Include user to get user name for cache invalidation
|
||||
.FirstOrDefault(a => a.Name == name);
|
||||
if (accountEntity != null)
|
||||
{
|
||||
@@ -37,6 +39,13 @@ public class PostgreSqlAccountRepository : IAccountRepository
|
||||
_cacheService.RemoveValue(keyCacheKey);
|
||||
_cacheService.RemoveValue(idCacheKey);
|
||||
_cacheService.RemoveValue("all_accounts");
|
||||
|
||||
// Invalidate user-specific accounts cache
|
||||
if (accountEntity.User != null)
|
||||
{
|
||||
var userAccountsCacheKey = $"accounts_user_{accountEntity.User.Name}";
|
||||
_cacheService.RemoveValue(userAccountsCacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,6 +199,10 @@ public class PostgreSqlAccountRepository : IAccountRepository
|
||||
// Invalidate all accounts cache since we added a new account
|
||||
_cacheService.RemoveValue("all_accounts");
|
||||
|
||||
// Invalidate user-specific accounts cache
|
||||
var userAccountsCacheKey = $"accounts_user_{account.User.Name}";
|
||||
_cacheService.RemoveValue(userAccountsCacheKey);
|
||||
|
||||
// Cache the new account for future lookups
|
||||
var nameCacheKey = $"account_{account.Name}";
|
||||
var keyCacheKey = $"account_key_{account.Key}";
|
||||
@@ -200,6 +213,41 @@ public class PostgreSqlAccountRepository : IAccountRepository
|
||||
_cacheService.SaveValue(idCacheKey, account, TimeSpan.FromHours(1));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Account>> GetAccountsByUserAsync(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Check cache first for accounts by user
|
||||
var cacheKey = $"accounts_user_{user.Name}";
|
||||
var cachedAccounts = _cacheService.GetValue<List<Account>>(cacheKey);
|
||||
if (cachedAccounts != null)
|
||||
{
|
||||
return cachedAccounts;
|
||||
}
|
||||
|
||||
await PostgreSqlConnectionHelper.EnsureConnectionOpenAsync(_context);
|
||||
|
||||
// Direct SQL query to fetch accounts by user ID - much more efficient than loading all accounts
|
||||
var accountEntities = await _context.Accounts
|
||||
.AsNoTracking()
|
||||
.Include(a => a.User)
|
||||
.Where(a => a.UserId == user.Id)
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var accounts = PostgreSqlMappers.Map(accountEntities).ToList();
|
||||
|
||||
// Cache accounts by user for 30 minutes since user accounts don't change frequently
|
||||
_cacheService.SaveValue(cacheKey, accounts, TimeSpan.FromMinutes(30));
|
||||
|
||||
return accounts;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await PostgreSqlConnectionHelper.SafeCloseConnectionAsync(_context);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateAccountAsync(Account account)
|
||||
{
|
||||
try
|
||||
@@ -229,6 +277,13 @@ public class PostgreSqlAccountRepository : IAccountRepository
|
||||
_cacheService.RemoveValue(keyCacheKey);
|
||||
_cacheService.RemoveValue(idCacheKey);
|
||||
_cacheService.RemoveValue("all_accounts");
|
||||
|
||||
// Invalidate user-specific accounts cache
|
||||
if (account.User != null)
|
||||
{
|
||||
var userAccountsCacheKey = $"accounts_user_{account.User.Name}";
|
||||
_cacheService.RemoveValue(userAccountsCacheKey);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -129,7 +129,27 @@ public static class PostgreSqlMappers
|
||||
AgentName = entity.AgentName,
|
||||
AvatarUrl = entity.AvatarUrl,
|
||||
TelegramChannel = entity.TelegramChannel,
|
||||
Id = entity.Id // Assuming Id is the primary key for UserEntity
|
||||
Id = entity.Id, // Assuming Id is the primary key for UserEntity
|
||||
Accounts = entity.Accounts?.Select(MapAccountWithoutUser).ToList() ?? new List<Account>()
|
||||
};
|
||||
}
|
||||
|
||||
// Helper method to map AccountEntity without User to avoid circular reference
|
||||
private static Account MapAccountWithoutUser(AccountEntity entity)
|
||||
{
|
||||
if (entity == null) return null;
|
||||
|
||||
return new Account
|
||||
{
|
||||
Id = entity.Id,
|
||||
Name = entity.Name,
|
||||
Exchange = entity.Exchange,
|
||||
Type = entity.Type,
|
||||
Key = entity.Key,
|
||||
Secret = entity.Secret,
|
||||
User = null, // Don't map User to avoid circular reference
|
||||
Balances = new List<Balance>(), // Empty list for now, balances handled separately if needed
|
||||
IsGmxInitialized = entity.IsGmxInitialized
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Users;
|
||||
using Managing.Infrastructure.Databases.PostgreSql.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -66,12 +67,12 @@ public class PostgreSqlUserRepository : BaseRepositoryWithLogging, IUserReposito
|
||||
}, nameof(GetUserByAgentNameAsync), ("agentName", agentName));
|
||||
}
|
||||
|
||||
public async Task<User> GetUserByNameAsync(string name)
|
||||
public async Task<User> GetUserByNameAsync(string name, bool fetchAccounts = false)
|
||||
{
|
||||
return await ExecuteWithLoggingAsync(async () =>
|
||||
{
|
||||
// Check cache first for frequently accessed users
|
||||
var cacheKey = $"user_name_{name}";
|
||||
var cacheKey = fetchAccounts ? $"user_name_with_accounts_{name}" : $"user_name_{name}";
|
||||
var cachedUser = _cacheService.GetValue<User>(cacheKey);
|
||||
if (cachedUser != null)
|
||||
{
|
||||
@@ -82,28 +83,61 @@ public class PostgreSqlUserRepository : BaseRepositoryWithLogging, IUserReposito
|
||||
{
|
||||
await PostgreSqlConnectionHelper.EnsureConnectionOpenAsync(_context);
|
||||
|
||||
// Optimized query with explicit SELECT to avoid loading unnecessary data
|
||||
var userEntity = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Name == name)
|
||||
.Select(u => new UserEntity
|
||||
User user;
|
||||
|
||||
if (fetchAccounts)
|
||||
{
|
||||
// Fetch user with accounts in a single query
|
||||
var userEntity = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Include(u => u.Accounts)
|
||||
.Where(u => u.Name == name)
|
||||
.FirstOrDefaultAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (userEntity == null)
|
||||
throw new InvalidOperationException($"User with name '{name}' not found");
|
||||
|
||||
user = PostgreSqlMappers.Map(userEntity);
|
||||
|
||||
// Map accounts using the existing mapper
|
||||
if (userEntity.Accounts != null)
|
||||
{
|
||||
Id = u.Id,
|
||||
Name = u.Name,
|
||||
AgentName = u.AgentName,
|
||||
AvatarUrl = u.AvatarUrl,
|
||||
TelegramChannel = u.TelegramChannel
|
||||
})
|
||||
.FirstOrDefaultAsync()
|
||||
.ConfigureAwait(false);
|
||||
user.Accounts = userEntity.Accounts.Select(PostgreSqlMappers.Map).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
user.Accounts = new List<Account>();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Optimized query with explicit SELECT to avoid loading unnecessary data
|
||||
var userEntity = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Name == name)
|
||||
.Select(u => new UserEntity
|
||||
{
|
||||
Id = u.Id,
|
||||
Name = u.Name,
|
||||
AgentName = u.AgentName,
|
||||
AvatarUrl = u.AvatarUrl,
|
||||
TelegramChannel = u.TelegramChannel
|
||||
})
|
||||
.FirstOrDefaultAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (userEntity == null)
|
||||
throw new InvalidOperationException($"User with name '{name}' not found");
|
||||
if (userEntity == null)
|
||||
throw new InvalidOperationException($"User with name '{name}' not found");
|
||||
|
||||
var user = PostgreSqlMappers.Map(userEntity);
|
||||
user = PostgreSqlMappers.Map(userEntity);
|
||||
user.Accounts = new List<Account>(); // Initialize empty list
|
||||
}
|
||||
|
||||
// Cache user for 5 minutes since user data doesn't change frequently
|
||||
_cacheService.SaveValue(cacheKey, user, TimeSpan.FromMinutes(5));
|
||||
// Use shorter cache time when including accounts since accounts change more frequently
|
||||
var cacheTime = fetchAccounts ? TimeSpan.FromMinutes(2) : TimeSpan.FromMinutes(5);
|
||||
_cacheService.SaveValue(cacheKey, user, cacheTime);
|
||||
|
||||
return user;
|
||||
}
|
||||
@@ -111,7 +145,7 @@ public class PostgreSqlUserRepository : BaseRepositoryWithLogging, IUserReposito
|
||||
{
|
||||
await PostgreSqlConnectionHelper.SafeCloseConnectionAsync(_context);
|
||||
}
|
||||
}, nameof(GetUserByNameAsync), ("name", name));
|
||||
}, nameof(GetUserByNameAsync), ("name", name), ("fetchAccounts", fetchAccounts));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<User>> GetAllUsersAsync()
|
||||
@@ -214,7 +248,9 @@ public class PostgreSqlUserRepository : BaseRepositoryWithLogging, IUserReposito
|
||||
|
||||
// Invalidate cache for updated user - handle both old and new AgentName
|
||||
var nameCacheKey = $"user_name_{user.Name}";
|
||||
var nameWithAccountsCacheKey = $"user_name_with_accounts_{user.Name}";
|
||||
_cacheService.RemoveValue(nameCacheKey);
|
||||
_cacheService.RemoveValue(nameWithAccountsCacheKey);
|
||||
|
||||
// Invalidate old AgentName cache if it existed
|
||||
if (!string.IsNullOrEmpty(oldAgentName))
|
||||
|
||||
Reference in New Issue
Block a user