Skip to content

Commit

Permalink
修改缓存拦截器前缀属性名称
Browse files Browse the repository at this point in the history
  • Loading branch information
UtilCore committed Nov 21, 2023
1 parent 91b79ec commit f51982f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>7</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionPatch>80</VersionPatch>
<VersionPatch>81</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
Expand Down
3 changes: 0 additions & 3 deletions src/Util.Caching.EasyCaching/CachingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public static List<ServerEndPoint> GetRedisEndPoints() {
/// 清理
/// </summary>
public static void Clear() {
if ( _redisEndPoints == null )
return;
_redisEndPoints.Clear();
_redisEndPoints = null;
}
}
24 changes: 18 additions & 6 deletions src/Util.Caching/CacheAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using Util.Aop;

namespace Util.Caching;
namespace Util.Caching;

/// <summary>
/// 缓存拦截器
/// </summary>
public class CacheAttribute : InterceptorBase {
/// <summary>
/// 缓存键前缀
/// 缓存键前缀,可使用占位符, {0} 表示第一个参数值,范例: User-{0}
/// </summary>
public string CacheKeyPrefix { get; set; }
public string Prefix { get; set; }
/// <summary>
/// 缓存过期间隔,单位:秒,默认值:36000
/// </summary>
Expand All @@ -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;
}
Expand All @@ -50,7 +50,19 @@ private Type GetReturnType( AspectContext context ) {
/// </summary>
private string CreateCacheKey( AspectContext context ) {
var keyGenerator = context.ServiceProvider.GetService<ICacheKeyGenerator>();
return keyGenerator.CreateCacheKey( context.ServiceMethod, context.Parameters, CacheKeyPrefix );
return keyGenerator.CreateCacheKey( context.ServiceMethod, context.Parameters, GetPrefix( context ) );
}

/// <summary>
/// 获取缓存键前缀
/// </summary>
private string GetPrefix( AspectContext context ) {
try {
return string.Format( Prefix, context.Parameters.ToArray() );
}
catch {
return Prefix;
}
}

/// <summary>
Expand All @@ -64,7 +76,7 @@ private async Task<object> GetCacheValue( ICache cache, Type returnType, string
/// 设置返回值
/// </summary>
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 } );
Expand Down

0 comments on commit f51982f

Please sign in to comment.