Abstract away cache. 2 implementations: redis and memory

This commit is contained in:
Kwoth
2022-06-23 13:07:45 +00:00
parent 1716c69132
commit 210da263ad
75 changed files with 11525 additions and 1547 deletions

View File

@@ -32,7 +32,7 @@ public static class ArrayExtensions
public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
=> Array.ConvertAll(arr, x => f(x));
public static IReadOnlyCollection<TOut> Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
public static TOut[] Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
{
var toReturn = new TOut[col.Count];

View File

@@ -37,8 +37,9 @@ public static class Extensions
_ => throw new ArgumentOutOfRangeException(nameof(text))
};
public static List<ulong> GetGuildIds(this DiscordSocketClient client)
=> client.Guilds.Select(x => x.Id).ToList();
public static ulong[] GetGuildIds(this DiscordSocketClient client)
=> client.Guilds
.Map(x => x.Id);
/// <summary>
/// Generates a string in the format HHH:mm if timespan is &gt;= 2m.

View File

@@ -9,10 +9,10 @@ namespace NadekoBot.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddBotStringsServices(this IServiceCollection services, int totalShards)
=> totalShards <= 1
public static IServiceCollection AddBotStringsServices(this IServiceCollection services, BotCacheImplemenation botCache)
=> botCache == BotCacheImplemenation.Memory
? services.AddSingleton<IStringsSource, LocalFileStringsSource>()
.AddSingleton<IBotStringsProvider, LocalBotStringsProvider>()
.AddSingleton<IBotStringsProvider, MemoryBotStringsProvider>()
.AddSingleton<IBotStrings, BotStrings>()
: services.AddSingleton<IStringsSource, LocalFileStringsSource>()
.AddSingleton<IBotStringsProvider, RedisBotStringsProvider>()
@@ -23,17 +23,6 @@ public static class ServiceCollectionExtensions
services.Scan(x => x.FromCallingAssembly()
.AddClasses(f => f.AssignableTo(typeof(ConfigServiceBase<>)))
.AsSelfWithInterfaces());
// var baseType = typeof(ConfigServiceBase<>);
//
// foreach (var type in Assembly.GetCallingAssembly().ExportedTypes.Where(x => x.IsSealed))
// {
// if (type.BaseType?.IsGenericType == true && type.BaseType.GetGenericTypeDefinition() == baseType)
// {
// services.AddSingleton(type);
// services.AddSingleton(x => (IConfigService)x.GetRequiredService(type));
// }
// }
return services;
}
@@ -48,7 +37,7 @@ public static class ServiceCollectionExtensions
.AddSingleton<ISoundcloudResolver, SoundcloudResolver>()
.AddSingleton<ILocalTrackResolver, LocalTrackResolver>()
.AddSingleton<IRadioResolver, RadioResolver>()
.AddSingleton<ITrackCacher, RedisTrackCacher>()
.AddSingleton<ITrackCacher, TrackCacher>()
.AddSingleton<YtLoader>()
.AddSingleton<IPlaceholderProvider>(svc => svc.GetRequiredService<IMusicService>());
@@ -65,10 +54,23 @@ public static class ServiceCollectionExtensions
return services;
}
public static IServiceCollection AddRedis(this IServiceCollection services, string redisOptions)
public static IServiceCollection AddCache(this IServiceCollection services, IBotCredentials creds)
{
var conf = ConfigurationOptions.Parse(redisOptions);
services.AddSingleton(ConnectionMultiplexer.Connect(conf));
return services;
if (creds.BotCache == BotCacheImplemenation.Redis)
{
var conf = ConfigurationOptions.Parse(creds.RedisOptions);
services.AddSingleton(ConnectionMultiplexer.Connect(conf))
.AddSingleton<IBotCache, RedisBotCache>()
.AddSingleton<IPubSub, RedisPubSub>();
}
else
{
services.AddSingleton<IBotCache, MemoryBotCache>()
.AddSingleton<IPubSub, EventPubSub>();
}
return services
.AddBotStringsServices(creds.BotCache);
}
}

View File

@@ -30,9 +30,9 @@ public static class UserExtensions
=> usr.AvatarId is null ? new(usr.GetDefaultAvatarUrl()) : new Uri(usr.GetAvatarUrl(ImageFormat.Auto, size));
// This method is only used for the xp card
public static Uri? RealAvatarUrl(this DiscordUser usr)
public static Uri RealAvatarUrl(this DiscordUser usr)
=> usr.AvatarId is null
? null
? new(CDN.GetDefaultUserAvatarUrl(ushort.Parse(usr.Discriminator)))
: new Uri(usr.AvatarId.StartsWith("a_", StringComparison.InvariantCulture)
? $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.gif"
: $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.png");