From 64c2e8c0e47c7877297cdba3a4c2c7653518a6f8 Mon Sep 17 00:00:00 2001 From: LemonNoCry Date: Wed, 31 Jul 2024 17:44:17 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20=E6=B7=BB=E5=8A=A0Cache=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Blog.Core.Api/Blog.Core.xml | 21 ++---- Blog.Core.Api/Controllers/ValuesController.cs | 66 +++++++++++-------- Blog.Core.Common/Core/InternalApp.cs | 10 +++ Blog.Core.Tests/Common_Test/CacheTest.cs | 37 +++++++++++ .../DependencyInjection/DI_Test.cs | 35 +++++++--- 5 files changed, 116 insertions(+), 53 deletions(-) create mode 100644 Blog.Core.Tests/Common_Test/CacheTest.cs diff --git a/Blog.Core.Api/Blog.Core.xml b/Blog.Core.Api/Blog.Core.xml index e3054f2..9d524e5 100644 --- a/Blog.Core.Api/Blog.Core.xml +++ b/Blog.Core.Api/Blog.Core.xml @@ -758,21 +758,6 @@ Values控制器 - - - ValuesController - - - - - - - - - - - - 测试Rabbit消息队列发送 @@ -900,6 +885,12 @@ + + + 测试缓存 + + + WeChatCompanyController diff --git a/Blog.Core.Api/Controllers/ValuesController.cs b/Blog.Core.Api/Controllers/ValuesController.cs index 03922d0..46797dc 100644 --- a/Blog.Core.Api/Controllers/ValuesController.cs +++ b/Blog.Core.Api/Controllers/ValuesController.cs @@ -19,6 +19,7 @@ using RabbitMQ.Client.Events; using System.ComponentModel.DataAnnotations; using System.Linq.Expressions; using System.Text; +using Blog.Core.Common.Caches.Interface; using Blog.Core.Common.Utility; namespace Blog.Core.Controllers @@ -45,29 +46,11 @@ namespace Blog.Core.Controllers private readonly IHttpPollyHelper _httpPollyHelper; private readonly IRabbitMQPersistentConnection _persistentConnection; private readonly SeqOptions _seqOptions; + private readonly ICaching _cache; - /// - /// ValuesController - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public ValuesController(IBlogArticleServices blogArticleServices - , IMapper mapper - , IAdvertisementServices advertisementServices - , Love love - , IRoleModulePermissionServices roleModulePermissionServices - , IUser user, IPasswordLibServices passwordLibServices - , IHttpPollyHelper httpPollyHelper - , IRabbitMQPersistentConnection persistentConnection - , IOptions seqOptions) + public ValuesController(IBlogArticleServices blogArticleServices, IMapper mapper, IAdvertisementServices advertisementServices, Love love, + IRoleModulePermissionServices roleModulePermissionServices, IUser user, IPasswordLibServices passwordLibServices, + IHttpPollyHelper httpPollyHelper, IRabbitMQPersistentConnection persistentConnection, IOptions seqOptions, ICaching caching) { // 测试 Authorize 和 mapper _mapper = mapper; @@ -85,6 +68,7 @@ namespace Blog.Core.Controllers // httpPolly _httpPollyHelper = httpPollyHelper; _persistentConnection = persistentConnection; + _cache = caching; _seqOptions = seqOptions.Value; } @@ -99,6 +83,7 @@ namespace Blog.Core.Controllers { _persistentConnection.TryConnect(); } + _persistentConnection.PublishMessage("Hello, RabbitMQ!", exchangeName: "blogcore", routingKey: "myRoutingKey"); return Ok(); } @@ -177,18 +162,18 @@ namespace Blog.Core.Controllers * 测试按照指定列查询 */ var queryByColums = await _blogArticleServices - .Query(it => new BlogViewModels() { btitle = it.btitle }); + .Query(it => new BlogViewModels() { btitle = it.btitle }); /* - * 测试按照指定列查询带多条件和排序方法 - */ + * 测试按照指定列查询带多条件和排序方法 + */ Expression> registerInfoWhere = a => a.btitle == "xxx" && a.bRemark == "XXX"; var queryByColumsByMultiTerms = await _blogArticleServices - .Query(it => new BlogArticle() { btitle = it.btitle }, registerInfoWhere, "bID Desc"); + .Query(it => new BlogArticle() { btitle = it.btitle }, registerInfoWhere, "bID Desc"); /* * 测试 sql 更新 - * + * * 【SQL参数】:@bID:5 * @bsubmitter:laozhang619 * @IsDeleted:False @@ -196,7 +181,7 @@ namespace Blog.Core.Controllers * `bsubmitter`=@bsubmitter,`IsDeleted`=@IsDeleted WHERE `bID`=@bID */ var updateSql = await _blogArticleServices.Update(new - { bsubmitter = $"laozhang{DateTime.Now.Millisecond}", IsDeleted = false, bID = 5 }); + { bsubmitter = $"laozhang{DateTime.Now.Millisecond}", IsDeleted = false, bID = 5 }); // 测试 AOP 缓存 @@ -484,6 +469,31 @@ namespace Blog.Core.Controllers { return IdGeneratorUtility.NextId(); } + + /// + /// 测试缓存 + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task> TestCacheAsync() + { + await _cache.SetAsync("test", "test", new TimeSpan(0, 10, 0)); + + var result = await _cache.GetAsync("test"); + if (!"test".Equals(result)) + { + return Failed("缓存失败,值不一样"); + } + + var count = _cache.GetAllCacheKeys().Count; + if (count <= 0) + { + return Failed("缓存失败,数量不对"); + } + + return Success(""); + } } public class ClaimDto diff --git a/Blog.Core.Common/Core/InternalApp.cs b/Blog.Core.Common/Core/InternalApp.cs index b8a7736..5cf3a97 100644 --- a/Blog.Core.Common/Core/InternalApp.cs +++ b/Blog.Core.Common/Core/InternalApp.cs @@ -42,4 +42,14 @@ public static class InternalApp { RootServices = app.Services; } + + public static void ConfigureApplication(this IServiceCollection services) + { + InternalServices = services; + } + + public static void ConfigureApplication(this IServiceProvider services) + { + RootServices = services; + } } \ No newline at end of file diff --git a/Blog.Core.Tests/Common_Test/CacheTest.cs b/Blog.Core.Tests/Common_Test/CacheTest.cs new file mode 100644 index 0000000..f3a7839 --- /dev/null +++ b/Blog.Core.Tests/Common_Test/CacheTest.cs @@ -0,0 +1,37 @@ +using Autofac; +using Blog.Core.Common; +using Blog.Core.Common.Caches.Interface; +using Xunit; +using Xunit.Abstractions; + +namespace Blog.Core.Tests.Common_Test; + +public class CacheTest +{ + private readonly ITestOutputHelper _testOutputHelper; + DI_Test dI_Test = new DI_Test(); + private readonly ICaching _cache; + + public CacheTest(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = testOutputHelper; + dI_Test.Build(); + _cache = App.GetService(); + } + + [Fact] + public void TestCaching() + { + _cache.Set("test", "test", new TimeSpan(0, 10, 0)); + + var result = _cache.Get("test"); + Assert.Equal("test", result); + + var caches = _cache.GetAllCacheKeys(); + _testOutputHelper.WriteLine(caches.ToJson()); + Assert.NotNull(caches); + + var count = _cache.GetAllCacheKeys().Count; + Assert.Equal(1, count); + } +} \ No newline at end of file diff --git a/Blog.Core.Tests/DependencyInjection/DI_Test.cs b/Blog.Core.Tests/DependencyInjection/DI_Test.cs index 1ac4d5a..bb037ef 100644 --- a/Blog.Core.Tests/DependencyInjection/DI_Test.cs +++ b/Blog.Core.Tests/DependencyInjection/DI_Test.cs @@ -18,6 +18,8 @@ using Microsoft.IdentityModel.Tokens; using System.Reflection; using System.Security.Claims; using System.Text; +using Blog.Core.Common.Core; +using Blog.Core.Extensions.ServiceExtensions; using Microsoft.Extensions.Logging; namespace Blog.Core.Tests @@ -51,7 +53,11 @@ namespace Blog.Core.Tests var basePath = AppContext.BaseDirectory; IServiceCollection services = new ServiceCollection(); + services.ConfigureApplication(); + + services.AddLogging(); services.AddAutoMapperSetup(); + services.AddCacheSetup(); services.AddSingleton(new AppSettings(basePath)); services.AddScoped(); @@ -99,11 +105,11 @@ namespace Blog.Core.Tests var builder = new ContainerBuilder(); //builder.RegisterType().As(); builder.RegisterInstance(new LoggerFactory()) - .As(); + .As(); builder.RegisterGeneric(typeof(Logger<>)) - .As(typeof(ILogger<>)) - .SingleInstance(); + .As(typeof(ILogger<>)) + .SingleInstance(); //指定已扫描程序集中的类型注册为提供所有其实现的接口。 builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency(); //注册仓储 @@ -118,15 +124,15 @@ namespace Blog.Core.Tests var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll"); var assemblysServices = Assembly.LoadFrom(servicesDllFile); builder.RegisterAssemblyTypes(assemblysServices) - .AsImplementedInterfaces() - .InstancePerLifetimeScope() - .PropertiesAutowired() - .EnableInterfaceInterceptors(); + .AsImplementedInterfaces() + .InstancePerLifetimeScope() + .PropertiesAutowired() + .EnableInterfaceInterceptors(); var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll"); var assemblysRepository = Assembly.LoadFrom(repositoryDllFile); builder.RegisterAssemblyTypes(assemblysRepository) - .PropertiesAutowired().AsImplementedInterfaces(); + .PropertiesAutowired().AsImplementedInterfaces(); services.Replace(ServiceDescriptor.Transient()); @@ -136,9 +142,18 @@ namespace Blog.Core.Tests builder.Populate(services); //使用已进行的组件登记创建新容器 - var ApplicationContainer = builder.Build(); + var applicationContainer = builder.Build(); + return applicationContainer; + } - return ApplicationContainer; + public IServiceProvider Build() + { + var container = DICollections(); + var serviceProvider = new AutofacServiceProvider(container); + serviceProvider.ConfigureApplication(); + App.IsBuild = true; + App.IsRun = true; + return serviceProvider; } } } \ No newline at end of file