From 7527f783d8018fede256b301bbb9c72545ce2d76 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sun, 25 Aug 2024 08:44:02 -0500 Subject: [PATCH] Remove LastSoldCount, Add LastIncreSoldCount. https://github.com/Qtoss-AI/QtossTransformer/pull/11 --- .../Infrastructures/SharpCacheAttribute.cs | 24 +++++++++++++------ .../Infrastructures/SharpCacheSettings.cs | 7 ++++++ .../BotSharp.Core/BotSharpCoreExtensions.cs | 8 ++++++- 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheSettings.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs index 957ff241..03bc70bb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheAttribute.cs @@ -10,19 +10,23 @@ public class SharpCacheAttribute : MoAttribute public static IServiceProvider Services { get; set; } = null!; private int _minutes; - private string _prefix; - public SharpCacheAttribute(int minutes, string prefix = "cache") + public SharpCacheAttribute(int minutes = 60) { _minutes = minutes; - _prefix = prefix; } public override void OnEntry(MethodContext context) { + var settings = Services.GetRequiredService(); + if (!settings.Enabled) + { + return; + } + var cache = Services.GetRequiredService(); - var key = GetCacheKey(context); + var key = GetCacheKey(settings, context); var value = cache.GetAsync(key, context.TaskReturnType).Result; if (value != null) { @@ -32,18 +36,24 @@ public class SharpCacheAttribute : MoAttribute public override void OnSuccess(MethodContext context) { + var settings = Services.GetRequiredService(); + if (!settings.Enabled) + { + return; + } + var cache = Services.GetRequiredService(); if (context.ReturnValue != null) { - var key = GetCacheKey(context); + var key = GetCacheKey(settings, context); cache.SetAsync(key, context.ReturnValue, new TimeSpan(0, _minutes, 0)).Wait(); } } - private string GetCacheKey(MethodContext context) + private string GetCacheKey(SharpCacheSettings settings, MethodContext context) { - var key = _prefix + "-" + context.Method.Name; + var key = settings.Prefix + "-" + context.Method.Name; foreach (var arg in context.Arguments) { if (arg is null) diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheSettings.cs new file mode 100644 index 00000000..2fc266d2 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/SharpCacheSettings.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Abstraction.Infrastructures; + +public class SharpCacheSettings +{ + public bool Enabled { get; set; } = false; + public string Prefix { get; set; } = "cache"; +} diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index 3d8503e1..f0a595d7 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -16,9 +16,15 @@ public static class BotSharpCoreExtensions { services.AddScoped(); services.AddScoped(); - services.AddSingleton(); + services.AddSingleton(); + // Register cache service + var cacheSettings = new SharpCacheSettings(); + config.Bind("SharpCache", cacheSettings); + services.AddSingleton(x => cacheSettings); + services.AddSingleton(); + RegisterPlugins(services, config); ConfigureBotSharpOptions(services, configOptions);