Rework caching
This does a *lot* of things. Essentially, it replaces the existing individual proxy- and autoproxy caches on the bot end with a global cache (in Core) that handles all the caching at once, and automatically invalidates the cache once something changes in the datastore. This allows us to do proxying and autoproxying with *zero database queries* (best-case).
This commit is contained in:
@@ -11,12 +11,10 @@ namespace PluralKit.Bot.Commands
|
||||
public class Autoproxy
|
||||
{
|
||||
private IDataStore _data;
|
||||
private AutoproxyCacheService _cache;
|
||||
|
||||
public Autoproxy(IDataStore data, AutoproxyCacheService cache)
|
||||
public Autoproxy(IDataStore data)
|
||||
{
|
||||
_data = data;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public async Task AutoproxyRoot(Context ctx)
|
||||
@@ -51,7 +49,6 @@ namespace PluralKit.Bot.Commands
|
||||
settings.AutoproxyMode = AutoproxyMode.Off;
|
||||
settings.AutoproxyMember = null;
|
||||
await _data.SetSystemGuildSettings(ctx.System, ctx.Guild.Id, settings);
|
||||
await _cache.FlushCacheForSystem(ctx.System, ctx.Guild.Id);
|
||||
await ctx.Reply($"{Emojis.Success} Autoproxy turned off in this server.");
|
||||
}
|
||||
}
|
||||
@@ -68,7 +65,6 @@ namespace PluralKit.Bot.Commands
|
||||
settings.AutoproxyMode = AutoproxyMode.Latch;
|
||||
settings.AutoproxyMember = null;
|
||||
await _data.SetSystemGuildSettings(ctx.System, ctx.Guild.Id, settings);
|
||||
await _cache.FlushCacheForSystem(ctx.System, ctx.Guild.Id);
|
||||
await ctx.Reply($"{Emojis.Success} Autoproxy set to latch mode in this server. Messages will now be autoproxied using the *last-proxied member* in this server.");
|
||||
}
|
||||
}
|
||||
@@ -85,7 +81,6 @@ namespace PluralKit.Bot.Commands
|
||||
settings.AutoproxyMode = AutoproxyMode.Front;
|
||||
settings.AutoproxyMember = null;
|
||||
await _data.SetSystemGuildSettings(ctx.System, ctx.Guild.Id, settings);
|
||||
await _cache.FlushCacheForSystem(ctx.System, ctx.Guild.Id);
|
||||
await ctx.Reply($"{Emojis.Success} Autoproxy set to front mode in this server. Messages will now be autoproxied using the *current first fronter*, if any.");
|
||||
}
|
||||
}
|
||||
@@ -98,7 +93,6 @@ namespace PluralKit.Bot.Commands
|
||||
settings.AutoproxyMode = AutoproxyMode.Member;
|
||||
settings.AutoproxyMember = member.Id;
|
||||
await _data.SetSystemGuildSettings(ctx.System, ctx.Guild.Id, settings);
|
||||
await _cache.FlushCacheForSystem(ctx.System, ctx.Guild.Id);
|
||||
await ctx.Reply($"{Emojis.Success} Autoproxy set to **{member.Name}** in this server.");
|
||||
}
|
||||
|
||||
|
@@ -10,14 +10,11 @@ namespace PluralKit.Bot.Commands
|
||||
{
|
||||
private IDataStore _data;
|
||||
private EmbedService _embeds;
|
||||
|
||||
private ProxyCacheService _proxyCache;
|
||||
|
||||
public Member(IDataStore data, EmbedService embeds, ProxyCacheService proxyCache)
|
||||
|
||||
public Member(IDataStore data, EmbedService embeds)
|
||||
{
|
||||
_data = data;
|
||||
_embeds = embeds;
|
||||
_proxyCache = proxyCache;
|
||||
}
|
||||
|
||||
public async Task NewMember(Context ctx) {
|
||||
@@ -51,8 +48,6 @@ namespace PluralKit.Bot.Commands
|
||||
await ctx.Reply($"{Emojis.Warn} You have reached the per-system member limit ({Limits.MaxMemberCount}). You will be unable to create additional members until existing members are deleted.");
|
||||
else if (memberCount >= Limits.MaxMembersWarnThreshold)
|
||||
await ctx.Reply($"{Emojis.Warn} You are approaching the per-system member limit ({memberCount} / {Limits.MaxMemberCount} members). Please review your member list for unused or duplicate members.");
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task MemberRandom(Context ctx)
|
||||
@@ -68,11 +63,8 @@ namespace PluralKit.Bot.Commands
|
||||
throw Errors.NoMembersError;
|
||||
var randInt = randGen.Next(members.Count);
|
||||
await ctx.Reply(embed: await _embeds.CreateMemberEmbed(ctx.System, members[randInt], ctx.Guild, ctx.LookupContextFor(ctx.System)));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task ViewMember(Context ctx, PKMember target)
|
||||
{
|
||||
var system = await _data.GetSystemById(target.System);
|
||||
|
@@ -10,12 +10,10 @@ namespace PluralKit.Bot.Commands
|
||||
public class MemberAvatar
|
||||
{
|
||||
private IDataStore _data;
|
||||
private ProxyCacheService _proxyCache;
|
||||
|
||||
public MemberAvatar(IDataStore data, ProxyCacheService proxyCache)
|
||||
public MemberAvatar(IDataStore data)
|
||||
{
|
||||
_data = data;
|
||||
_proxyCache = proxyCache;
|
||||
}
|
||||
|
||||
public async Task Avatar(Context ctx, PKMember target)
|
||||
@@ -80,8 +78,6 @@ namespace PluralKit.Bot.Commands
|
||||
await ctx.Reply($"{Emojis.Success} Member avatar changed to attached image. Please note that if you delete the message containing the attachment, the avatar will stop working.");
|
||||
}
|
||||
// No-arguments no-attachment case covered by conditional at the very top
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,12 +11,10 @@ namespace PluralKit.Bot.Commands
|
||||
public class MemberEdit
|
||||
{
|
||||
private IDataStore _data;
|
||||
private ProxyCacheService _proxyCache;
|
||||
|
||||
public MemberEdit(IDataStore data, ProxyCacheService proxyCache)
|
||||
public MemberEdit(IDataStore data)
|
||||
{
|
||||
_data = data;
|
||||
_proxyCache = proxyCache;
|
||||
}
|
||||
|
||||
public async Task Name(Context ctx, PKMember target) {
|
||||
@@ -50,8 +48,6 @@ namespace PluralKit.Bot.Commands
|
||||
if (memberGuildConfig.DisplayName != null)
|
||||
await ctx.Reply($"{Emojis.Note} Note that this member has a server name set ({memberGuildConfig.DisplayName.SanitizeMentions()}) in this server ({ctx.Guild.Name.SanitizeMentions()}), and will be proxied using that name here.");
|
||||
}
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task Description(Context ctx, PKMember target) {
|
||||
@@ -141,8 +137,6 @@ namespace PluralKit.Bot.Commands
|
||||
}
|
||||
|
||||
await ctx.Reply(successStr);
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task ServerName(Context ctx, PKMember target)
|
||||
@@ -168,8 +162,6 @@ namespace PluralKit.Bot.Commands
|
||||
successStr += $"Member server name cleared. This member will now be proxied using their member name \"{target.Name.SanitizeMentions()}\" in this server ({ctx.Guild.Name.SanitizeMentions()}).";
|
||||
|
||||
await ctx.Reply(successStr);
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task KeepProxy(Context ctx, PKMember target)
|
||||
@@ -190,7 +182,6 @@ namespace PluralKit.Bot.Commands
|
||||
await ctx.Reply($"{Emojis.Success} Member proxy tags will now be included in the resulting message when proxying.");
|
||||
else
|
||||
await ctx.Reply($"{Emojis.Success} Member proxy tags will now not be included in the resulting message when proxying.");
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task Privacy(Context ctx, PKMember target)
|
||||
@@ -222,8 +213,6 @@ namespace PluralKit.Bot.Commands
|
||||
if (!await ctx.ConfirmWithReply(target.Hid)) throw Errors.MemberDeleteCancelled;
|
||||
await _data.DeleteMember(target);
|
||||
await ctx.Reply($"{Emojis.Success} Member deleted.");
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,12 +8,10 @@ namespace PluralKit.Bot.Commands
|
||||
public class MemberProxy
|
||||
{
|
||||
private IDataStore _data;
|
||||
private ProxyCacheService _proxyCache;
|
||||
|
||||
public MemberProxy(IDataStore data, ProxyCacheService proxyCache)
|
||||
public MemberProxy(IDataStore data)
|
||||
{
|
||||
_data = data;
|
||||
_proxyCache = proxyCache;
|
||||
}
|
||||
|
||||
public async Task Proxy(Context ctx, PKMember target)
|
||||
@@ -117,9 +115,6 @@ namespace PluralKit.Bot.Commands
|
||||
await _data.SaveMember(target);
|
||||
await ctx.Reply($"{Emojis.Success} Member proxy tags set to `{requestedTag.ProxyString.SanitizeMentions()}`.");
|
||||
}
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -17,13 +17,11 @@ namespace PluralKit.Bot.Commands
|
||||
{
|
||||
private IDataStore _data;
|
||||
private EmbedService _embeds;
|
||||
private ProxyCacheService _proxyCache;
|
||||
|
||||
public SystemEdit(IDataStore data, EmbedService embeds, ProxyCacheService proxyCache)
|
||||
public SystemEdit(IDataStore data, EmbedService embeds)
|
||||
{
|
||||
_data = data;
|
||||
_embeds = embeds;
|
||||
_proxyCache = proxyCache;
|
||||
}
|
||||
|
||||
public async Task Name(Context ctx)
|
||||
@@ -62,8 +60,6 @@ namespace PluralKit.Bot.Commands
|
||||
|
||||
await _data.SaveSystem(ctx.System);
|
||||
await ctx.Reply($"{Emojis.Success} System tag {(newTag != null ? $"changed. Member names will now end with `{newTag.SanitizeMentions()}` when proxied" : "cleared")}.");
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task Avatar(Context ctx)
|
||||
@@ -115,8 +111,6 @@ namespace PluralKit.Bot.Commands
|
||||
var embed = url != null ? new EmbedBuilder().WithImageUrl(url).Build() : null;
|
||||
await ctx.Reply($"{Emojis.Success} System avatar changed.", embed: embed);
|
||||
}
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task Delete(Context ctx) {
|
||||
@@ -128,8 +122,6 @@ namespace PluralKit.Bot.Commands
|
||||
|
||||
await _data.DeleteSystem(ctx.System);
|
||||
await ctx.Reply($"{Emojis.Success} System deleted.");
|
||||
|
||||
await _proxyCache.InvalidateResultsForSystem(ctx.System);
|
||||
}
|
||||
|
||||
public async Task SystemProxy(Context ctx)
|
||||
|
Reference in New Issue
Block a user