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); _dbContext.ClientPairs.RemoveRange(ownPairData);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
var otherPairData = await _dbContext.ClientPairs.Include(u => u.User) var otherPairData = await _dbContext.ClientPairs.Include(u => u.User)
.Where(u => u.OtherUser.UID == user.UID).AsNoTracking().ToListAsync().ConfigureAwait(false); .Where(u => u.OtherUser.UID == user.UID).AsNoTracking().ToListAsync().ConfigureAwait(false);
foreach (var pair in otherPairData) foreach (var pair in otherPairData)
@@ -64,7 +63,6 @@ public partial class MareHub
_dbContext.GroupPairPreferredPermissions.RemoveRange(groupPermissions); _dbContext.GroupPairPreferredPermissions.RemoveRange(groupPermissions);
_dbContext.Permissions.RemoveRange(individualPermissions); _dbContext.Permissions.RemoveRange(individualPermissions);
_dbContext.GroupBans.RemoveRange(bannedEntries); _dbContext.GroupBans.RemoveRange(bannedEntries);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_mareMetrics.IncCounter(MetricsAPI.CounterUsersRegisteredDeleted, 1); _mareMetrics.IncCounter(MetricsAPI.CounterUsersRegisteredDeleted, 1);
@@ -240,7 +238,6 @@ public partial class MareHub
} }
await _dbContext.SaveChangesAsync().ConfigureAwait(false); await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_cacheService.MarkAsStale(userUid, null); _cacheService.MarkAsStale(userUid, null);
_logger.LogCallInfo(MareHubLogger.Args(dto, "Success")); _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(); var notPinned = groupPairs.Where(g => !g.IsPinned && !g.IsModerator).ToList();
_dbContext.GroupPairs.RemoveRange(notPinned); _dbContext.GroupPairs.RemoveRange(notPinned);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
foreach (var user in notPinned)
{
_cacheService.MarkAsStale(user.GroupUserUID, null);
}
foreach (var pair in notPinned) foreach (var pair in notPinned)
{ {
@@ -231,6 +226,12 @@ public partial class MareHub
await UserGroupLeave(groupUserPair, pairIdent, pair.GroupUserUID).ConfigureAwait(false); 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")] [Authorize(Policy = "Identified")]
@@ -472,7 +473,6 @@ public partial class MareHub
} }
await _dbContext.GroupPairs.AddAsync(newPair).ConfigureAwait(false); await _dbContext.GroupPairs.AddAsync(newPair).ConfigureAwait(false);
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
_logger.LogCallInfo(MareHubLogger.Args(aliasOrGid, "Success")); _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); 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); var userPairsAfterJoin = await _cacheService.GetAllPairs(UserUID, _dbContext, true).ConfigureAwait(false);
foreach (var pair in groupPairs)
{
_cacheService.MarkAsStale(UserUID, pair.GroupUserUID);
}
var userPairsAfterJoin = await _cacheService.GetAllPairs(UserUID, _dbContext).ConfigureAwait(false);
foreach (var pair in groupPairs) 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) foreach (var pair in groupPairs)
{ {
_cacheService.MarkAsStale(UserUID, pair.GroupUserUID); _cacheService.MarkAsStale(UserUID, pair.GroupUserUID);
} }
await _dbContext.SaveChangesAsync().ConfigureAwait(false);
return true; return true;
} }
@@ -613,25 +606,27 @@ public partial class MareHub
_logger.LogCallInfo(MareHubLogger.Args(dto, "Success")); _logger.LogCallInfo(MareHubLogger.Args(dto, "Success"));
_dbContext.GroupPairs.Remove(groupPair); _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(); 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 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); 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); await Clients.User(dto.User.UID).Client_GroupDelete(new GroupDto(dto.Group)).ConfigureAwait(false);
foreach (var groupUserPair in groupPairs) foreach (var groupUserPair in groupPairs)
{ {
await UserGroupLeave(groupUserPair, userIdent, dto.User.UID).ConfigureAwait(false); await UserGroupLeave(groupUserPair, userIdent, dto.User.UID).ConfigureAwait(false);
_cacheService.MarkAsStale(dto.User.UID, groupUserPair.GroupUserUID);
} }
} }
[Authorize(Policy = "Identified")] [Authorize(Policy = "Identified")]

View File

@@ -171,7 +171,7 @@ public partial class MareHub
{ {
_logger.LogCallInfo(); _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 pairs.Select(p =>
{ {
return new UserFullPairDto(new UserData(p.Key, p.Value.Alias), return new UserFullPairDto(new UserData(p.Key, p.Value.Alias),

View File

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

View File

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

View File

@@ -7,25 +7,25 @@ public class UserPermissionQuery
public string Alias { get; set; } public string Alias { get; set; }
public string GID { get; set; } public string GID { get; set; }
public bool Synced { get; set; } public bool Synced { get; set; }
public bool OwnpermIsPaused { get; set; } public bool? OwnpermIsPaused { get; set; }
public bool OwnpermSticky { get; set; } public bool? OwnpermSticky { get; set; }
public bool OwnpermDisableAnimations { get; set; } public bool? OwnpermDisableAnimations { get; set; }
public bool OwnpermDisableSounds { get; set; } public bool? OwnpermDisableSounds { get; set; }
public bool OwnpermDisableVFX { get; set; } public bool? OwnpermDisableVFX { get; set; }
public bool? OtherpermIsPaused { get; set; } public bool? OtherpermIsPaused { get; set; }
public bool? OtherpermDisableAnimations { get; set; } public bool? OtherpermDisableAnimations { get; set; }
public bool? OtherpermDisableSounds { get; set; } public bool? OtherpermDisableSounds { get; set; }
public bool? OtherpermDisableVFX { get; set; } public bool? OtherpermDisableVFX { get; set; }
public UserPermissionSet OwnPermissions => new UserPermissionSet public UserPermissionSet? OwnPermissions => OwnpermSticky == null ? null : new UserPermissionSet
{ {
UserUID = UserUID, UserUID = UserUID,
OtherUserUID = OtherUserUID, OtherUserUID = OtherUserUID,
IsPaused = OwnpermIsPaused, IsPaused = OwnpermIsPaused.Value,
DisableAnimations = OwnpermDisableAnimations, DisableAnimations = OwnpermDisableAnimations.Value,
DisableSounds = OwnpermDisableSounds, DisableSounds = OwnpermDisableSounds.Value,
DisableVFX = OwnpermDisableVFX, DisableVFX = OwnpermDisableVFX.Value,
Sticky = OwnpermSticky Sticky = OwnpermSticky.Value
}; };
public UserPermissionSet? OtherPermissions => !Synced ? null : new UserPermissionSet public UserPermissionSet? OtherPermissions => !Synced ? null : new UserPermissionSet

View File

@@ -20,8 +20,6 @@ public static class SharedDbFunctions
newOwner = potentialNewOwner.GroupUserUID; newOwner = potentialNewOwner.GroupUserUID;
potentialNewOwner.IsPinned = true; potentialNewOwner.IsPinned = true;
potentialNewOwner.IsModerator = false; potentialNewOwner.IsModerator = false;
await context.SaveChangesAsync().ConfigureAwait(false);
break; break;
} }
} }
@@ -30,8 +28,6 @@ public static class SharedDbFunctions
{ {
context.GroupPairs.RemoveRange(groupPairs); context.GroupPairs.RemoveRange(groupPairs);
context.Groups.Remove(group); context.Groups.Remove(group);
await context.SaveChangesAsync().ConfigureAwait(false);
} }
return (groupHasMigrated, newOwner); return (groupHasMigrated, newOwner);
@@ -96,8 +92,6 @@ public static class SharedDbFunctions
} }
dbContext.GroupPairs.Remove(userGroupPair); dbContext.GroupPairs.Remove(userGroupPair);
await dbContext.SaveChangesAsync().ConfigureAwait(false);
} }
var defaultPermissions = await dbContext.UserDefaultPreferredPermissions.Where(u => u.UserUID == user.UID).ToListAsync().ConfigureAwait(false); var defaultPermissions = await dbContext.UserDefaultPreferredPermissions.Where(u => u.UserUID == user.UID).ToListAsync().ConfigureAwait(false);