diff --git a/Behaver.cs b/Behaver.cs index 6e79d4d..3bfcec6 100644 --- a/Behaver.cs +++ b/Behaver.cs @@ -194,7 +194,7 @@ public class Behaver Console.WriteLine($"couldn't find protocol for {message.Channel?.Id}"); return 404; } -Console.WriteLine("I remember this message, i have found a protocol, i am ready to react toit"); + Console.WriteLine("I remember this message, i have found a protocol, i am ready to react toit"); return await iprotocol.React(message, reaction); } public async Task Reply(Guid messageId, string text) diff --git a/Models/Channel.cs b/Models/Channel.cs index 3ebbed8..6c06651 100644 --- a/Models/Channel.cs +++ b/Models/Channel.cs @@ -20,6 +20,7 @@ public class Channel public List SubChannels { get; set; } [JsonIgnore] public Channel ParentChannel { get; set; } + public Guid? ParentChannelId { get; set; } public string Protocol { get; set; } [DeleteBehavior(DeleteBehavior.Cascade)] public List Messages { get; set; } diff --git a/Rememberer.cs b/Rememberer.cs index 59d4509..018c819 100644 --- a/Rememberer.cs +++ b/Rememberer.cs @@ -8,6 +8,25 @@ public static class Rememberer { private static readonly SemaphoreSlim dbAccessSemaphore = new(1, 1); private static readonly ChattingContext db = new(); + private static List channels; + private static bool channelCacheDirty = true; + + private static void cacheChannels() + { + dbAccessSemaphore.Wait(); + channels = db.Channels.ToList(); + foreach (Channel ch in channels) + { + if (ch.ParentChannelId != null) + { + ch.ParentChannel = channels.FirstOrDefault(c => c.Id == ch.ParentChannelId); + ch.ParentChannel.SubChannels ??= []; + ch.ParentChannel.SubChannels.Add(ch); + } + } + channelCacheDirty = false; + dbAccessSemaphore.Release(); + } public static Account SearchAccount(Expression> predicate) { @@ -33,13 +52,11 @@ public static class Rememberer dbAccessSemaphore.Release(); return toReturn; } - public static Channel SearchChannel(Expression> predicate) + public static Channel SearchChannel(Func predicate) { - Channel toReturn; - dbAccessSemaphore.Wait(); - toReturn = db.Channels.FirstOrDefault(predicate); - dbAccessSemaphore.Release(); - return toReturn; + if(channelCacheDirty) + Task.Run(() => cacheChannels()).Wait(); + return channels.FirstOrDefault(predicate); } public static Message SearchMessage(Expression> predicate) { @@ -75,10 +92,14 @@ public static class Rememberer } public static Channel RememberChannel(Channel toRemember) { + if(channelCacheDirty) + Task.Run(() => cacheChannels()).Wait(); //so we always do 2 db trips? dbAccessSemaphore.Wait(); db.Update(toRemember); db.SaveChanges(); dbAccessSemaphore.Release(); + channelCacheDirty = true; + cacheChannels(); return toRemember; } public static void RememberMessage(Message toRemember) @@ -202,14 +223,9 @@ public static class Rememberer } public static Channel ChannelDetail(Guid Id) { - Channel toReturn; - dbAccessSemaphore.Wait(); - toReturn = db.Channels.Find(Id); - dbAccessSemaphore.Release(); - return toReturn; - // .Include(u => u.SubChannels) - // .Include(u => u.Users) - // .Include(u => u.ParentChannel); + if(channelCacheDirty) + Task.Run(() => cacheChannels()).Wait(); + return channels.Find(c => c.Id == Id); } public static Message MessageDetail(Guid Id) {