Add test for executor
This commit is contained in:
@@ -67,6 +67,54 @@ public class PostgreSqlUserRepository : BaseRepositoryWithLogging, IUserReposito
|
||||
}, nameof(GetUserByAgentNameAsync), ("agentName", agentName));
|
||||
}
|
||||
|
||||
public async Task<User?> GetUserByIdAsync(int userId)
|
||||
{
|
||||
return await ExecuteWithLoggingAsync(async () =>
|
||||
{
|
||||
// Check cache first for frequently accessed users
|
||||
var cacheKey = $"user_id_{userId}";
|
||||
var cachedUser = _cacheService.GetValue<User>(cacheKey);
|
||||
if (cachedUser != null)
|
||||
{
|
||||
return cachedUser;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await PostgreSqlConnectionHelper.EnsureConnectionOpenAsync(_context);
|
||||
|
||||
// Optimized query with explicit SELECT to avoid loading unnecessary data
|
||||
var userEntity = await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Id == userId)
|
||||
.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)
|
||||
return null;
|
||||
|
||||
var user = PostgreSqlMappers.Map(userEntity);
|
||||
|
||||
// Cache user for 5 minutes since user data doesn't change frequently
|
||||
_cacheService.SaveValue(cacheKey, user, TimeSpan.FromMinutes(5));
|
||||
|
||||
return user;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await PostgreSqlConnectionHelper.SafeCloseConnectionAsync(_context);
|
||||
}
|
||||
}, nameof(GetUserByIdAsync), ("userId", userId));
|
||||
}
|
||||
|
||||
public async Task<User> GetUserByNameAsync(string name, bool fetchAccounts = false)
|
||||
{
|
||||
return await ExecuteWithLoggingAsync(async () =>
|
||||
@@ -237,8 +285,10 @@ 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}";
|
||||
var idCacheKey = $"user_id_{user.Id}";
|
||||
_cacheService.RemoveValue(nameCacheKey);
|
||||
_cacheService.RemoveValue(nameWithAccountsCacheKey);
|
||||
_cacheService.RemoveValue(idCacheKey);
|
||||
|
||||
// Invalidate old AgentName cache if it existed
|
||||
if (!string.IsNullOrEmpty(oldAgentName))
|
||||
|
||||
Reference in New Issue
Block a user