forked from adam/discord-bot-shtik
cache all the channels.
see #25 - now after restarts, we can still reply and message, etc.
This commit is contained in:
parent
0ff3902fd0
commit
9ad7520c61
@ -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<int> Reply(Guid messageId, string text)
|
||||
|
@ -20,6 +20,7 @@ public class Channel
|
||||
public List<Channel> SubChannels { get; set; }
|
||||
[JsonIgnore]
|
||||
public Channel ParentChannel { get; set; }
|
||||
public Guid? ParentChannelId { get; set; }
|
||||
public string Protocol { get; set; }
|
||||
[DeleteBehavior(DeleteBehavior.Cascade)]
|
||||
public List<Message> Messages { get; set; }
|
||||
|
@ -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<Channel> 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<Func<Account, bool>> predicate)
|
||||
{
|
||||
@ -33,13 +52,11 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Channel SearchChannel(Expression<Func<Channel, bool>> predicate)
|
||||
public static Channel SearchChannel(Func<Channel, bool> 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<Func<Message, bool>> 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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user