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 } );