From aed0b8c2e5017fa3950283b32b935abd039679bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 7 Jan 2023 19:41:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../appsettings.json | 7 ++-- .../Yi.Framework.Common/Base/NullValue.cs | 2 +- .../Cache/Aop}/CacheAOPbase.cs | 2 +- .../Cache/Aop}/MemoryCacheAOP.cs | 11 +++-- .../Cache/Aop}/RedisCacheAOP.cs | 2 +- .../Yi.Framework.Core.csproj | 1 + .../AutoFacExtend/CustomAutofacModule.cs | 42 ++++++++++++------- 7 files changed, 40 insertions(+), 27 deletions(-) rename Yi.Framework.Net6/{Yi.Framework.WebCore/AOP => Yi.Framework.Core/Cache/Aop}/CacheAOPbase.cs (98%) rename Yi.Framework.Net6/{Yi.Framework.WebCore/AOP => Yi.Framework.Core/Cache/Aop}/MemoryCacheAOP.cs (84%) rename Yi.Framework.Net6/{Yi.Framework.WebCore/AOP => Yi.Framework.Core/Cache/Aop}/RedisCacheAOP.cs (98%) diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json index 11add86a..13bd024a 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json @@ -48,10 +48,9 @@ //选择缓存 "CacheSelect": "MemoryCache", - //RedisCacheAOP - "RedisCacheAOP_Enabled": false, - //MemoryCacheAOP - "MemoryCacheAOP_Enabled": false, + //缓存Aop + "CacheAOP_Enabled": false, + //缓存种子数据是否开启 "CacheSeed_Enabled": false, diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Base/NullValue.cs b/Yi.Framework.Net6/Yi.Framework.Common/Base/NullValue.cs index 208a3c02..62e91eae 100644 --- a/Yi.Framework.Net6/Yi.Framework.Common/Base/NullValue.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Base/NullValue.cs @@ -112,7 +112,7 @@ namespace Yi.Framework.Common.Base public static string TryStringNull(this object value) { - return value == null ? "" : value.ToString().Trim(); + return value == null ? "" : value.ToString()!.Trim(); } /// diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AOP/CacheAOPbase.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/CacheAOPbase.cs similarity index 98% rename from Yi.Framework.Net6/Yi.Framework.WebCore/AOP/CacheAOPbase.cs rename to Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/CacheAOPbase.cs index a350bf73..e123b52f 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AOP/CacheAOPbase.cs +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/CacheAOPbase.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; using Yi.Framework.Common.Base; using Yi.Framework.Common.Helper; -namespace Yi.Framework.WebCore.AOP +namespace Yi.Framework.Core.Cache.Aop { public abstract class CacheAOPbase : IInterceptor { diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AOP/MemoryCacheAOP.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/MemoryCacheAOP.cs similarity index 84% rename from Yi.Framework.Net6/Yi.Framework.WebCore/AOP/MemoryCacheAOP.cs rename to Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/MemoryCacheAOP.cs index 6eb88550..b347c0ee 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AOP/MemoryCacheAOP.cs +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/MemoryCacheAOP.cs @@ -1,10 +1,13 @@ -using System; +using Castle.DynamicProxy; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Common.Attribute; +using Yi.Framework.Core.Cache; -namespace Yi.Framework.WebCore.AOP +namespace Yi.Framework.Core.Cache.Aop { public class MemoryCacheAOP : CacheAOPbase { @@ -18,8 +21,8 @@ namespace Yi.Framework.WebCore.AOP { var method = invocation.MethodInvocationTarget ?? invocation.Method; - var CachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)); - if (CachingAttribute is CachingAttribute qCachingAttribute) + var cachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)); + if (cachingAttribute is CachingAttribute qCachingAttribute) { //获取自定义缓存键 var cacheKey = CustomCacheKey(invocation); diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AOP/RedisCacheAOP.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/RedisCacheAOP.cs similarity index 98% rename from Yi.Framework.Net6/Yi.Framework.WebCore/AOP/RedisCacheAOP.cs rename to Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/RedisCacheAOP.cs index 5abae70c..53038ac8 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AOP/RedisCacheAOP.cs +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/Aop/RedisCacheAOP.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; using Yi.Framework.Core.Cache; using Yi.Framework.Common.Attribute; -namespace Yi.Framework.WebCore.AOP +namespace Yi.Framework.Core.Cache.Aop { public class RedisCacheAOP : CacheAOPbase { diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj b/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj index a28444ff..d6598f4f 100644 --- a/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj +++ b/Yi.Framework.Net6/Yi.Framework.Core/Yi.Framework.Core.csproj @@ -12,6 +12,7 @@ + diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs index 3e0e8638..2b76f188 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs @@ -12,11 +12,11 @@ using System.Linq; using System.Reflection; using System.Threading.Tasks; using Yi.Framework.Common.Abstract; +using Yi.Framework.Core.Cache.Aop; using Yi.Framework.Interface; using Yi.Framework.Job; using Yi.Framework.Repository; using Yi.Framework.Service; -using Yi.Framework.WebCore.AOP; using Yi.Framework.WebCore.AutoFacExtend; using Yi.Framework.WebCore.CommonExtend; using Yi.Framework.WebCore.Impl; @@ -46,25 +46,35 @@ namespace Yi.Framework.WebCore.AutoFacExtend containerBuilder.RegisterType().As().SingleInstance(); var cacheType = new List(); - if (Appsettings.appBool("RedisCacheAOP_Enabled")) - { - containerBuilder.RegisterType(); - cacheType.Add(typeof(RedisCacheAOP)); - } - if (Appsettings.appBool("MemoryCacheAOP_Enabled")) - { - containerBuilder.RegisterType(); - cacheType.Add(typeof(MemoryCacheAOP)); - } + //containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); //containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope(); ///反射注入服务层及接口层 var assemblysServices = GetDll("Yi.Framework.Service.dll"); - containerBuilder.RegisterAssemblyTypes(assemblysServices).PropertiesAutowired(new AutowiredPropertySelector()) - .AsImplementedInterfaces() - .InstancePerLifetimeScope() - .EnableInterfaceInterceptors() - .InterceptedBy(cacheType.ToArray()); + var regContainerBuilder = containerBuilder.RegisterAssemblyTypes(assemblysServices).PropertiesAutowired(new AutowiredPropertySelector()) + .AsImplementedInterfaces() + .InstancePerLifetimeScope() + .EnableInterfaceInterceptors(); + + if (Appsettings.appBool("CacheAOP_Enabled")) + { + var cacheSelect = Appsettings.app("CacheSelect"); + + switch (cacheSelect) + { + case "Redis": + containerBuilder.RegisterType(); + cacheType.Add(typeof(RedisCacheAOP)); + break; + case "MemoryCache": + containerBuilder.RegisterType(); + cacheType.Add(typeof(MemoryCacheAOP)); + break; + default: throw new ArgumentException("CacheSelect配置填的是什么东西?俺不认得"); + } + regContainerBuilder.InterceptedBy(cacheType.ToArray()); + } + //开启工作单元拦截 //.InterceptedBy(typeof(UnitOfWorkInterceptor));