feat(bot): add Redis cache

This commit is contained in:
spiral
2022-04-13 08:44:53 -04:00
parent fadf007abc
commit c2094e3b7a
9 changed files with 560 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ public class BotConfig
public string? GatewayQueueUrl { get; set; }
public bool UseRedisRatelimiter { get; set; } = false;
public bool UseRedisCache { get; set; } = false;
public string? RedisGatewayUrl { get; set; }

View File

@@ -2,6 +2,7 @@ using Autofac;
using Microsoft.Extensions.Configuration;
using Myriad.Cache;
using Myriad.Gateway;
using Myriad.Types;
using Myriad.Rest;
@@ -51,6 +52,10 @@ public class Init
if (config.UseRedisRatelimiter)
await redis.InitAsync(coreConfig);
var cache = services.Resolve<IDiscordCache>();
if (cache is RedisDiscordCache)
await (cache as RedisDiscordCache).InitAsync(coreConfig.RedisAddr, config.ClientId!.Value);
if (config.Cluster == null)
{
// "Connect to the database" (ie. set off database migrations and ensure state)

View File

@@ -44,7 +44,13 @@ public class BotModule: Module
}).AsSelf().SingleInstance();
builder.RegisterType<Cluster>().AsSelf().SingleInstance();
builder.RegisterType<RedisGatewayService>().AsSelf().SingleInstance();
builder.Register(c => { return new MemoryDiscordCache(); }).AsSelf().As<IDiscordCache>().SingleInstance();
builder.Register<IDiscordCache>(c => {
var botConfig = c.Resolve<BotConfig>();
if (botConfig.UseRedisCache)
return new RedisDiscordCache(c.Resolve<ILogger>());
return new MemoryDiscordCache();
}).AsSelf().SingleInstance();
builder.RegisterType<PrivateChannelService>().AsSelf().SingleInstance();
builder.Register(c =>

View File

@@ -1520,6 +1520,8 @@
"myriad": {
"type": "Project",
"dependencies": {
"Google.Protobuf": "3.13.0",
"Grpc.Net.ClientFactory": "2.32.0",
"Polly": "7.2.1",
"Polly.Contrib.WaitAndRetry": "1.1.1",
"Serilog": "2.10.0",