From de427d8bfe7ea7152cf30e746c54ef170e21638c Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 3 Feb 2020 14:47:29 +0100 Subject: [PATCH] Properly invalidate account caches when deleting systems --- PluralKit.Core/ProxyCache.cs | 11 +++++++++++ PluralKit.Core/Stores.cs | 12 ++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/PluralKit.Core/ProxyCache.cs b/PluralKit.Core/ProxyCache.cs index deb42c59..3b9a6fd8 100644 --- a/PluralKit.Core/ProxyCache.cs +++ b/PluralKit.Core/ProxyCache.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -26,6 +27,16 @@ namespace PluralKit.Core public Task InvalidateSystem(PKSystem system) => InvalidateSystem(system.Id); + public void InvalidateDeletedSystem(int systemId, IEnumerable accounts) + { + // Used when the system's already removed so we can't look up accounts + // We assume the account list is saved already somewhere and can be passed here (which is the case in Store) + + _cache.Remove(KeyForSystem(systemId)); + foreach (var account in accounts) + _cache.Remove(KeyForAccount(account)); + } + public async Task InvalidateSystem(int systemId) { if (_cache.TryGetValue(KeyForSystem(systemId), out var systemCache)) diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index ef05a8b5..721a584c 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -540,12 +540,16 @@ namespace PluralKit { await _cache.InvalidateSystem(system); } - public async Task DeleteSystem(PKSystem system) { - using (var conn = await _conn.Obtain()) - await conn.ExecuteAsync("delete from systems where id = @Id", system); + public async Task DeleteSystem(PKSystem system) + { + using var conn = await _conn.Obtain(); + + // Fetch the list of accounts *before* deletion so we can cache-bust all of those + var accounts = (await conn.QueryAsync("select uid from accounts where system = @Id", system)).ToArray(); + await conn.ExecuteAsync("delete from systems where id = @Id", system); _logger.Information("Deleted system {System}", system.Id); - await _cache.InvalidateSystem(system); + _cache.InvalidateDeletedSystem(system.Id, accounts); } public async Task> GetSystemAccounts(PKSystem system)