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
@ -20,6 +20,7 @@ public class Channel
|
|||||||
public List<Channel> SubChannels { get; set; }
|
public List<Channel> SubChannels { get; set; }
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public Channel ParentChannel { get; set; }
|
public Channel ParentChannel { get; set; }
|
||||||
|
public Guid? ParentChannelId { get; set; }
|
||||||
public string Protocol { get; set; }
|
public string Protocol { get; set; }
|
||||||
[DeleteBehavior(DeleteBehavior.Cascade)]
|
[DeleteBehavior(DeleteBehavior.Cascade)]
|
||||||
public List<Message> Messages { get; set; }
|
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 SemaphoreSlim dbAccessSemaphore = new(1, 1);
|
||||||
private static readonly ChattingContext db = new();
|
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)
|
public static Account SearchAccount(Expression<Func<Account, bool>> predicate)
|
||||||
{
|
{
|
||||||
@ -33,13 +52,11 @@ public static class Rememberer
|
|||||||
dbAccessSemaphore.Release();
|
dbAccessSemaphore.Release();
|
||||||
return toReturn;
|
return toReturn;
|
||||||
}
|
}
|
||||||
public static Channel SearchChannel(Expression<Func<Channel, bool>> predicate)
|
public static Channel SearchChannel(Func<Channel, bool> predicate)
|
||||||
{
|
{
|
||||||
Channel toReturn;
|
if(channelCacheDirty)
|
||||||
dbAccessSemaphore.Wait();
|
Task.Run(() => cacheChannels()).Wait();
|
||||||
toReturn = db.Channels.FirstOrDefault(predicate);
|
return channels.FirstOrDefault(predicate);
|
||||||
dbAccessSemaphore.Release();
|
|
||||||
return toReturn;
|
|
||||||
}
|
}
|
||||||
public static Message SearchMessage(Expression<Func<Message, bool>> predicate)
|
public static Message SearchMessage(Expression<Func<Message, bool>> predicate)
|
||||||
{
|
{
|
||||||
@ -75,10 +92,14 @@ public static class Rememberer
|
|||||||
}
|
}
|
||||||
public static Channel RememberChannel(Channel toRemember)
|
public static Channel RememberChannel(Channel toRemember)
|
||||||
{
|
{
|
||||||
|
if(channelCacheDirty)
|
||||||
|
Task.Run(() => cacheChannels()).Wait(); //so we always do 2 db trips?
|
||||||
dbAccessSemaphore.Wait();
|
dbAccessSemaphore.Wait();
|
||||||
db.Update(toRemember);
|
db.Update(toRemember);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
dbAccessSemaphore.Release();
|
dbAccessSemaphore.Release();
|
||||||
|
channelCacheDirty = true;
|
||||||
|
cacheChannels();
|
||||||
return toRemember;
|
return toRemember;
|
||||||
}
|
}
|
||||||
public static void RememberMessage(Message toRemember)
|
public static void RememberMessage(Message toRemember)
|
||||||
@ -202,14 +223,9 @@ public static class Rememberer
|
|||||||
}
|
}
|
||||||
public static Channel ChannelDetail(Guid Id)
|
public static Channel ChannelDetail(Guid Id)
|
||||||
{
|
{
|
||||||
Channel toReturn;
|
if(channelCacheDirty)
|
||||||
dbAccessSemaphore.Wait();
|
Task.Run(() => cacheChannels()).Wait();
|
||||||
toReturn = db.Channels.Find(Id);
|
return channels.Find(c => c.Id == Id);
|
||||||
dbAccessSemaphore.Release();
|
|
||||||
return toReturn;
|
|
||||||
// .Include(u => u.SubChannels)
|
|
||||||
// .Include(u => u.Users)
|
|
||||||
// .Include(u => u.ParentChannel);
|
|
||||||
}
|
}
|
||||||
public static Message MessageDetail(Guid Id)
|
public static Message MessageDetail(Guid Id)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user