some fixes on cache creation stuff I think

This commit is contained in:
rootdarkarchon
2023-10-18 09:33:27 +02:00
parent e4a6657391
commit 6fc2f1e90c
7 changed files with 43 additions and 49 deletions

View File

@@ -44,7 +44,6 @@ public partial class MareHub
}
_dbContext.ClientPairs.RemoveRange(ownPairData);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherPairData = await _dbContext.ClientPairs.Include(u => u.User)
.Where(u => u.OtherUser.UID == user.UID).AsNoTracking().ToListAsync().ConfigureAwait(false);
foreach (var pair in otherPairData)
@@ -64,7 +63,6 @@ public partial class MareHub
_dbContext.GroupPairPreferredPermissions.RemoveRange(groupPermissions);
_dbContext.Permissions.RemoveRange(individualPermissions);
_dbContext.GroupBans.RemoveRange(bannedEntries);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_mareMetrics.IncCounter(MetricsAPI.CounterUsersRegisteredDeleted, 1);
@@ -240,7 +238,6 @@ public partial class MareHub
}
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_cacheService.MarkAsStale(userUid, null);
_logger.LogCallInfo(MareHubLogger.Args(dto, "Success"));

View File

@@ -212,11 +212,6 @@ public partial class MareHub
var notPinned = groupPairs.Where(g => !g.IsPinned && !g.IsModerator).ToList();
_dbContext.GroupPairs.RemoveRange(notPinned);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
foreach (var user in notPinned)
{
_cacheService.MarkAsStale(user.GroupUserUID, null);
}
foreach (var pair in notPinned)
{
@@ -231,6 +226,12 @@ public partial class MareHub
await UserGroupLeave(groupUserPair, pairIdent, pair.GroupUserUID).ConfigureAwait(false);
}
}
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
foreach (var user in notPinned)
{
_cacheService.MarkAsStale(user.GroupUserUID, null);
}
}
[Authorize(Policy = "Identified")]
@@ -472,7 +473,6 @@ public partial class MareHub
}
await _dbContext.GroupPairs.AddAsync(newPair).ConfigureAwait(false);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_logger.LogCallInfo(MareHubLogger.Args(aliasOrGid, "Success"));
@@ -485,13 +485,7 @@ public partial class MareHub
var groupPairs = await _dbContext.GroupPairs.Include(p => p.GroupUser).Where(p => p.GroupGID == group.GID && p.GroupUserUID != UserUID).ToListAsync().ConfigureAwait(false);
_cacheService.MarkAsStale(UserUID, null);
foreach (var pair in groupPairs)
{
_cacheService.MarkAsStale(UserUID, pair.GroupUserUID);
}
var userPairsAfterJoin = await _cacheService.GetAllPairs(UserUID, _dbContext).ConfigureAwait(false);
var userPairsAfterJoin = await _cacheService.GetAllPairs(UserUID, _dbContext, true).ConfigureAwait(false);
foreach (var pair in groupPairs)
{
@@ -581,14 +575,13 @@ public partial class MareHub
}
}
_cacheService.MarkAsStale(UserUID, null);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
foreach (var pair in groupPairs)
{
_cacheService.MarkAsStale(UserUID, pair.GroupUserUID);
}
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
return true;
}
@@ -613,25 +606,27 @@ public partial class MareHub
_logger.LogCallInfo(MareHubLogger.Args(dto, "Success"));
_dbContext.GroupPairs.Remove(groupPair);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_cacheService.MarkAsStale(dto.User.UID, null);
foreach (var user in await _dbContext.GroupPairs.Where(u => u.GroupGID == dto.GID).ToListAsync().ConfigureAwait(false))
{
_cacheService.MarkAsStale(user.GroupUserUID, null);
}
var groupPairs = _dbContext.GroupPairs.Where(p => p.GroupGID == group.GID).AsNoTracking().ToList();
await Clients.Users(groupPairs.Select(p => p.GroupUserUID)).Client_GroupPairLeft(dto).ConfigureAwait(false);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var userIdent = await GetUserIdent(dto.User.UID).ConfigureAwait(false);
if (userIdent == null) return;
if (userIdent == null)
{
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
return;
}
await Clients.User(dto.User.UID).Client_GroupDelete(new GroupDto(dto.Group)).ConfigureAwait(false);
foreach (var groupUserPair in groupPairs)
{
await UserGroupLeave(groupUserPair, userIdent, dto.User.UID).ConfigureAwait(false);
_cacheService.MarkAsStale(dto.User.UID, groupUserPair.GroupUserUID);
}
}
[Authorize(Policy = "Identified")]

View File

@@ -171,7 +171,7 @@ public partial class MareHub
{
_logger.LogCallInfo();
var pairs = await _cacheService.GetAllPairs(UserUID, _dbContext).ConfigureAwait(false);
var pairs = await _cacheService.GetAllPairs(UserUID, _dbContext, true).ConfigureAwait(false);
return pairs.Select(p =>
{
return new UserFullPairDto(new UserData(p.Key, p.Value.Alias),

View File

@@ -175,8 +175,6 @@ public class UserCleanupService : IHostedService
}
dbContext.GroupPairs.Remove(userGroupPair);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
_logger.LogInformation("User purged: {uid}", user.UID);

View File

@@ -25,8 +25,14 @@ public class UserPairCacheService : IHostedService
_cache.TryRemove(uid, out _);
}
public async Task<Dictionary<string, UserInfo>> GetAllPairs(string uid, MareDbContext dbContext)
public async Task<Dictionary<string, UserInfo>> GetAllPairs(string uid, MareDbContext dbContext, bool ignoreCache = false)
{
if (ignoreCache)
{
_cache[uid] = await BuildFullCache(dbContext, uid).ConfigureAwait(false);
return _cache[uid];
}
await WaitForProcessing(uid).ConfigureAwait(false);
if (!_cache.ContainsKey(uid))
@@ -99,7 +105,7 @@ public class UserPairCacheService : IHostedService
{
var pairs = await dbContext.GetAllPairsForUser(uid).ToListAsync().ConfigureAwait(false);
return pairs.GroupBy(g => g.OtherUserUID, StringComparer.Ordinal)
var pairsDict = pairs.GroupBy(g => g.OtherUserUID, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g =>
{
return new UserInfo(g.First().Alias,
@@ -109,6 +115,8 @@ public class UserPairCacheService : IHostedService
g.First().OwnPermissions,
g.First().OtherPermissions);
}, StringComparer.OrdinalIgnoreCase);
return pairsDict.Where(p => p.Value.OwnPermissions != null).ToDictionary(u => u.Key, u => u.Value, StringComparer.Ordinal);
}
private async Task<UserInfo?> BuildIndividualCache(MareDbContext dbContext, string uid, string otheruid)
@@ -118,12 +126,14 @@ public class UserPairCacheService : IHostedService
if (!pairs.Any()) return null;
var groups = pairs.Select(g => g.GID).ToList();
return new UserInfo(pairs[0].Alias,
var userInfo = new UserInfo(pairs[0].Alias,
pairs.SingleOrDefault(p => string.IsNullOrEmpty(p.GID))?.Synced ?? false,
pairs.Max(p => p.Synced),
pairs.Select(p => string.IsNullOrEmpty(p.GID) ? Constants.IndividualKeyword : p.GID).ToList(),
pairs[0].OwnPermissions,
pairs[0].OtherPermissions);
return userInfo.OwnPermissions == null ? null : userInfo;
}
private async Task ProcessStaleEntries()