From f51982f74284a60c12b875b1d82fbb07dd233cba Mon Sep 17 00:00:00 2001 From: root <8907060@qq.com> Date: Tue, 21 Nov 2023 10:45:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=93=E5=AD=98=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=99=A8=E5=89=8D=E7=BC=80=E5=B1=9E=E6=80=A7=E5=90=8D?= =?UTF-8?q?=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/version.props | 2 +- .../CachingOptions.cs | 3 --- src/Util.Caching/CacheAttribute.cs | 24 ++++++++++++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/build/version.props b/build/version.props index 8a0101485..0f71bc65c 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 7 1 - 80 + 81 $(VersionMajor).$(VersionMinor).$(VersionPatch) diff --git a/src/Util.Caching.EasyCaching/CachingOptions.cs b/src/Util.Caching.EasyCaching/CachingOptions.cs index 038e37f1b..d5384cc2a 100644 --- a/src/Util.Caching.EasyCaching/CachingOptions.cs +++ b/src/Util.Caching.EasyCaching/CachingOptions.cs @@ -46,9 +46,6 @@ public static List GetRedisEndPoints() { /// 清理 /// public static void Clear() { - if ( _redisEndPoints == null ) - return; - _redisEndPoints.Clear(); _redisEndPoints = null; } } \ No newline at end of file diff --git a/src/Util.Caching/CacheAttribute.cs b/src/Util.Caching/CacheAttribute.cs index e2aa91028..a3b9a0389 100644 --- a/src/Util.Caching/CacheAttribute.cs +++ b/src/Util.Caching/CacheAttribute.cs @@ -1,15 +1,15 @@ using Util.Aop; -namespace Util.Caching; +namespace Util.Caching; /// /// 缓存拦截器 /// public class CacheAttribute : InterceptorBase { /// - /// 缓存键前缀 + /// 缓存键前缀,可使用占位符, {0} 表示第一个参数值,范例: User-{0} /// - public string CacheKeyPrefix { get; set; } + public string Prefix { get; set; } /// /// 缓存过期间隔,单位:秒,默认值:36000 /// @@ -23,7 +23,7 @@ public override async Task Invoke( AspectContext context, AspectDelegate next ) var returnType = GetReturnType( context ); var key = CreateCacheKey( context ); var value = await GetCacheValue( cache, returnType, key ); - if ( value != null ) { + if( value != null ) { SetReturnValue( context, returnType, value ); return; } @@ -50,7 +50,19 @@ private Type GetReturnType( AspectContext context ) { /// private string CreateCacheKey( AspectContext context ) { var keyGenerator = context.ServiceProvider.GetService(); - return keyGenerator.CreateCacheKey( context.ServiceMethod, context.Parameters, CacheKeyPrefix ); + return keyGenerator.CreateCacheKey( context.ServiceMethod, context.Parameters, GetPrefix( context ) ); + } + + /// + /// 获取缓存键前缀 + /// + private string GetPrefix( AspectContext context ) { + try { + return string.Format( Prefix, context.Parameters.ToArray() ); + } + catch { + return Prefix; + } } /// @@ -64,7 +76,7 @@ private async Task GetCacheValue( ICache cache, Type returnType, string /// 设置返回值 /// private void SetReturnValue( AspectContext context, Type returnType, object value ) { - if ( context.IsAsync() ) { + if( context.IsAsync() ) { context.ReturnValue = typeof( Task ).GetMethods() .First( p => p.Name == "FromResult" && p.ContainsGenericParameters ) .MakeGenericMethod( returnType ).Invoke( null, new[] { value } );