From cd3677687ee57cf9ffc838eb89c0a6c375bceb9d Mon Sep 17 00:00:00 2001 From: wosperry <593281239@qq.com> Date: Mon, 7 Mar 2022 18:09:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Redis=E7=BC=93=E5=AD=98=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=B8=BA=E8=BF=87=E6=9C=9F=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CaptchaServiceCollectionExtensions.cs | 11 ++++++---- .../RedisCacheCaptchaExtensions.cs | 20 ++++++++++--------- Lazy.Captcha.Web/Program.cs | 8 +++++++- Lazy.Captcha.Web/appsettings.json | 2 +- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs b/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs index e3143d0..66ab4c7 100644 --- a/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs +++ b/Lazy.Captcha.Core/CaptchaServiceCollectionExtensions.cs @@ -29,15 +29,18 @@ namespace Microsoft.Extensions.DependencyInjection if (optionsAction != null) services.PostConfigure(optionsAction); services.TryAdd(ServiceDescriptor.Scoped()); + + services.TryAdd(ServiceDescriptor.Scoped()); + services.AddScoped(); + services.AddDistributedMemoryCache(); + return services; } + [Obsolete("请直接使用 AddCaptcha 方法,已更改为默认注册内存缓存", false)] public static IServiceCollection AddMemoryCacheCaptcha(this IServiceCollection services, IConfiguration configuration, Action optionsAction = default!) { - return services - .AddDistributedMemoryCache() - .AddScoped() - .AddCaptcha(configuration, optionsAction); + return services.AddCaptcha(configuration, optionsAction); } } } \ No newline at end of file diff --git a/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs b/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs index 4209a08..5a5b48e 100644 --- a/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs +++ b/Lazy.Captcha.Redis/RedisCacheCaptchaExtensions.cs @@ -10,21 +10,23 @@ namespace Microsoft.Extensions.DependencyInjection { public static class RedisCacheCaptchaExtensions { + /// + /// 即将弃用,调整为直接使用 AddCache(),并且配置 AddStackExchangeRedisCache,参考 https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed?view=aspnetcore-6.0#distributed-redis-cache + /// + [Obsolete("调整为直接使用 AddCache(),使用 Redis 缓存时配置 AddStackExchangeRedisCache,参考 https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed?view=aspnetcore-6.0#distributed-redis-cache",false)] public static IServiceCollection AddRedisCacheCaptcha(this IServiceCollection services, IConfiguration configuration, Action optionsAction = default!, string connName = null!) { - var connStr = configuration.GetConnectionString(connName??"RedisCache"); + var connStr = configuration.GetConnectionString(connName ?? "RedisCache"); if (string.IsNullOrEmpty(connStr)) { throw new Exception("Please set connection string for key `RedisCache` in setting json file"); } - return services - .AddStackExchangeRedisCache(options => - { - options.Configuration = connStr; - options.InstanceName = "captcha:"; - }) - .AddScoped() - .AddCaptcha(configuration, optionsAction); + services.AddCaptcha(configuration, optionsAction); + return services.AddStackExchangeRedisCache(options => + { + options.Configuration = connStr; + options.InstanceName = "captcha:"; + }); } } } \ No newline at end of file diff --git a/Lazy.Captcha.Web/Program.cs b/Lazy.Captcha.Web/Program.cs index 42f573b..85a4397 100644 --- a/Lazy.Captcha.Web/Program.cs +++ b/Lazy.Captcha.Web/Program.cs @@ -7,7 +7,7 @@ var builder = WebApplication.CreateBuilder(args); //builder.Services.AddRedisCacheCaptcha(builder.Configuration); // ڴ洢 appsettings.json -builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option => +builder.Services.AddRedisCacheCaptcha(builder.Configuration, option => { // ÿԸappsettings.json @@ -17,6 +17,12 @@ builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option => //option.ImageOption.BubbleCount = 0; }); +//builder.Services.AddStackExchangeRedisCache(options => +//{ +// options.Configuration = builder.Configuration.GetConnectionString("RedisCache"); +// options.InstanceName = "captcha:"; +//}); + // ȫòڴ //builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option => //{ diff --git a/Lazy.Captcha.Web/appsettings.json b/Lazy.Captcha.Web/appsettings.json index 0188f44..bfac394 100644 --- a/Lazy.Captcha.Web/appsettings.json +++ b/Lazy.Captcha.Web/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "RedisCache": "localhost,password=Aa123456." + "RedisCache": "wosperry.com.cn,password=Aa123456." }, "CaptchaOptions": { "CaptchaType": 5, // 验证码类型 0 - 11 -- Gitee From 7fc7669417e70de5fdcca635a50feb321d297135 Mon Sep 17 00:00:00 2001 From: wosperry <593281239@qq.com> Date: Mon, 7 Mar 2022 18:10:57 +0800 Subject: [PATCH 2/3] appsettings --- Lazy.Captcha.Web/appsettings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lazy.Captcha.Web/appsettings.json b/Lazy.Captcha.Web/appsettings.json index bfac394..0188f44 100644 --- a/Lazy.Captcha.Web/appsettings.json +++ b/Lazy.Captcha.Web/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "RedisCache": "wosperry.com.cn,password=Aa123456." + "RedisCache": "localhost,password=Aa123456." }, "CaptchaOptions": { "CaptchaType": 5, // 验证码类型 0 - 11 -- Gitee From 53594175c77b5c397e20695ba76e957e51e7c14e Mon Sep 17 00:00:00 2001 From: wosperry <593281239@qq.com> Date: Mon, 7 Mar 2022 18:15:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?program.cs=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Lazy.Captcha.Web/Program.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Lazy.Captcha.Web/Program.cs b/Lazy.Captcha.Web/Program.cs index 85a4397..e1842cc 100644 --- a/Lazy.Captcha.Web/Program.cs +++ b/Lazy.Captcha.Web/Program.cs @@ -3,26 +3,23 @@ using Lazy.Captcha.Core.Generator; var builder = WebApplication.CreateBuilder(args); -//// redis洢appsettings.json + //builder.Services.AddRedisCacheCaptcha(builder.Configuration); // ڴ洢 appsettings.json -builder.Services.AddRedisCacheCaptcha(builder.Configuration, option => -{ - // ÿԸappsettings.json - - //option.ImageOption.Animation = false; - //option.CaptchaType = CaptchaType.WORD_UPPER; - //option.ImageOption.FontFamily = DefaultFontFamilys.Instance.Robot; - //option.ImageOption.BubbleCount = 0; -}); +builder.Services.AddCaptcha(builder.Configuration); +// ʹredis //builder.Services.AddStackExchangeRedisCache(options => //{ // options.Configuration = builder.Configuration.GetConnectionString("RedisCache"); // options.InstanceName = "captcha:"; //}); + + + +// ----------------------------------------------------------------------------- // ȫòڴ //builder.Services.AddMemoryCacheCaptcha(builder.Configuration, option => //{ -- Gitee