什么是AOP
www.baidu.com
更新Redis緩存接口
在之前的redis緩存博客中我們定義了redis操作接口和實現,在實際項目開發中,我又對它進行了修改,主要是增加了異步和批量刪除的接口。修改Common下的Redis文件夾的IRedisCacheManager文件和RedisCacheManager增加以下接口和方法。
IRedisCacheManager:
//獲取 Reids 緩存值 Task<string> GetValueAsync(string key); //獲取值,並序列化 Task<TEntity> GetAsync<TEntity>(string key); //保存 Task SetAsync(string key, object value, TimeSpan cacheTime); //判斷是否存在 Task<bool> GetAsync(string key); //移除某一個緩存值 Task RemoveAsync(string key); //根據關鍵字移除 Task RemoveByKey(string key); //全部清除 Task ClearAsync();
RedisCacheManager:
public async Task ClearAsync() { foreach (var endPoint in this.GetRedisConnection().GetEndPoints()) { var server = this.GetRedisConnection().GetServer(endPoint); foreach (var key in server.Keys()) { await redisConnection.GetDatabase().KeyDeleteAsync(key); } } } public async Task<bool> GetAsync(string key) { return await redisConnection.GetDatabase().KeyExistsAsync(key); } public async Task<string> GetValueAsync(string key) { return await redisConnection.GetDatabase().StringGetAsync(key); } public async Task<TEntity> GetAsync<TEntity>(string key) { var value = await redisConnection.GetDatabase().StringGetAsync(key); if (value.HasValue) { //需要用的反序列化,將Redis存儲的Byte[],進行反序列化 return SerializeHelper.Deserialize<TEntity>(value); } else { return default; } } public async Task RemoveAsync(string key) { await redisConnection.GetDatabase().KeyDeleteAsync(key); } public async Task RemoveByKey(string key) { var redisResult = await redisConnection.GetDatabase().ScriptEvaluateAsync(LuaScript.Prepare( //Redis的keys模糊查詢: " local res = redis.call('KEYS', @keypattern) " + " return res "), new { @keypattern = key }); if (!redisResult.IsNull) { var keys = (string[])redisResult; foreach (var k in keys) redisConnection.GetDatabase().KeyDelete(k); } } public async Task SetAsync(string key, object value, TimeSpan cacheTime) { if (value != null) { //序列化,將object值生成RedisValue await redisConnection.GetDatabase().StringSetAsync(key, SerializeHelper.Serialize(value), cacheTime); } } public async Task<bool> SetValueAsync(string key, byte[] value) { return await redisConnection.GetDatabase().StringSetAsync(key, value, TimeSpan.FromSeconds(120)); }
定義緩存特性
Common層新增Attributes文件夾,並新建CachingAttribute來定義我們的redis緩存特性,有了這個特性,我們就可以控制方法是否需要緩存到redis。
/// <summary> /// 這個Attribute就是使用時候的驗證,把它添加到要緩存數據的方法中,即可完成緩存的操作。 /// </summary> [AttributeUsage(AttributeTargets.Method, Inherited = true)] public class CachingAttribute : Attribute { //過期時間 public int AbsoluteExpiration { get; set; } /// <summary> /// 自定義key /// </summary> public string CustomKeyValue { get; set; } /// <summary> /// 是否刪除 /// </summary> public bool IsDelete { get; set; } = false; }
定義Redis切面攔截器
在主工程新加AOP文件夾,新建CacheAOPbase和RedisCacheAOP文件

public abstract class CacheAOPbase : IInterceptor { /// <summary> /// AOP的攔截方法 /// </summary> /// <param name="invocation"></param> public abstract void Intercept(IInvocation invocation); /// <summary> /// 自定義緩存的key /// </summary> /// <param name="invocation"></param> /// <returns></returns> protected string CustomCacheKey(IInvocation invocation) { var typeName = invocation.TargetType.Name; var methodName = invocation.Method.Name; var methodArguments = invocation.Arguments.Select(GetArgumentValue).Take(3).ToList();//獲取參數列表,最多三個 string key = $"{typeName}:{methodName}:"; foreach (var param in methodArguments) { key = $"{key}{param}:"; } return key.TrimEnd(':'); } /// <summary> /// object 轉 string /// </summary> /// <param name="arg"></param> /// <returns></returns> protected static string GetArgumentValue(object arg) { if (arg is DateTime || arg is DateTime?) return ((DateTime)arg).ToString("yyyyMMddHHmmss"); if (arg is string || arg is ValueType || arg is Nullable) return arg.ToString(); if (arg != null) { if (arg is Expression) { var obj = arg as Expression; var result = Resolve(obj); return Common.Helper.MD5Helper.MD5Encrypt16(result); } else if (arg.GetType().IsClass) { return Common.Helper.MD5Helper.MD5Encrypt16(Newtonsoft.Json.JsonConvert.SerializeObject(arg)); } } return string.Empty; } private static string Resolve(Expression expression) { if (expression is LambdaExpression) { LambdaExpression lambda = expression as LambdaExpression; expression = lambda.Body; return Resolve(expression); } if (expression is BinaryExpression) { BinaryExpression binary = expression as BinaryExpression; if (binary.Left is MemberExpression && binary.Right is ConstantExpression)//解析x=>x.Name=="123" x.Age==123這類 return ResolveFunc(binary.Left, binary.Right, binary.NodeType); if (binary.Left is MethodCallExpression && binary.Right is ConstantExpression)//解析x=>x.Name.Contains("xxx")==false這類的 { object value = (binary.Right as ConstantExpression).Value; return ResolveLinqToObject(binary.Left, value, binary.NodeType); } if ((binary.Left is MemberExpression && binary.Right is MemberExpression) || (binary.Left is MemberExpression && binary.Right is UnaryExpression))//解析x=>x.Date==DateTime.Now這種 { LambdaExpression lambda = Expression.Lambda(binary.Right); Delegate fn = lambda.Compile(); ConstantExpression value = Expression.Constant(fn.DynamicInvoke(null), binary.Right.Type); return ResolveFunc(binary.Left, value, binary.NodeType); } } if (expression is UnaryExpression) { UnaryExpression unary = expression as UnaryExpression; if (unary.Operand is MethodCallExpression)//解析!x=>x.Name.Contains("xxx")或!array.Contains(x.Name)這類 return ResolveLinqToObject(unary.Operand, false); if (unary.Operand is MemberExpression && unary.NodeType == ExpressionType.Not)//解析x=>!x.isDeletion這樣的 { ConstantExpression constant = Expression.Constant(false); return ResolveFunc(unary.Operand, constant, ExpressionType.Equal); } } if (expression is MemberExpression && expression.NodeType == ExpressionType.MemberAccess)//解析x=>x.isDeletion這樣的 { MemberExpression member = expression as MemberExpression; ConstantExpression constant = Expression.Constant(true); return ResolveFunc(member, constant, ExpressionType.Equal); } if (expression is MethodCallExpression)//x=>x.Name.Contains("xxx")或array.Contains(x.Name)這類 { MethodCallExpression methodcall = expression as MethodCallExpression; return ResolveLinqToObject(methodcall, true); } //已經修改過代碼body應該不會是null值了 if (!(expression is BinaryExpression body)) return string.Empty; var Operator = GetOperator(body.NodeType); var Left = Resolve(body.Left); var Right = Resolve(body.Right); string Result = string.Format("({0} {1} {2})", Left, Operator, Right); return Result; } private static string GetOperator(ExpressionType expressiontype) { return expressiontype switch { ExpressionType.And => "and", ExpressionType.AndAlso => "and", ExpressionType.Or => "or", ExpressionType.OrElse => "or", ExpressionType.Equal => "=", ExpressionType.NotEqual => "<>", ExpressionType.LessThan => "<", ExpressionType.LessThanOrEqual => "<=", ExpressionType.GreaterThan => ">", ExpressionType.GreaterThanOrEqual => ">=", _ => throw new Exception(string.Format("不支持{0}此種運算符查找!" + expressiontype)), }; } private static string ResolveFunc(Expression left, Expression right, ExpressionType expressiontype) { var Name = (left as MemberExpression).Member.Name; var Value = (right as ConstantExpression).Value; var Operator = GetOperator(expressiontype); return Name + Operator + Value ?? "null"; } private static string ResolveLinqToObject(Expression expression, object value, ExpressionType? expressiontype = null) { var MethodCall = expression as MethodCallExpression; var MethodName = MethodCall.Method.Name; switch (MethodName) { case "Contains": if (MethodCall.Object != null) return Like(MethodCall); return In(MethodCall, value); case "Count": return Len(MethodCall, value, expressiontype.Value); case "LongCount": return Len(MethodCall, value, expressiontype.Value); default: throw new Exception(string.Format("不支持{0}方法的查找!", MethodName)); } } private static string In(MethodCallExpression expression, object isTrue) { var Argument1 = (expression.Arguments[0] as MemberExpression).Expression as ConstantExpression; var Argument2 = expression.Arguments[1] as MemberExpression; var Field_Array = Argument1.Value.GetType().GetFields().First(); object[] Array = Field_Array.GetValue(Argument1.Value) as object[]; List<string> SetInPara = new List<string>(); for (int i = 0; i < Array.Length; i++) { string Value = Array[i].ToString(); SetInPara.Add(Value); } string Name = Argument2.Member.Name; string Operator = Convert.ToBoolean(isTrue) ? "in" : " not in"; string CompName = string.Join(",", SetInPara); string Result = string.Format("{0} {1} ({2})", Name, Operator, CompName); return Result; } private static string Like(MethodCallExpression expression) { var Temp = expression.Arguments[0]; LambdaExpression lambda = Expression.Lambda(Temp); Delegate fn = lambda.Compile(); var tempValue = Expression.Constant(fn.DynamicInvoke(null), Temp.Type); string Value = string.Format("%{0}%", tempValue); string Name = (expression.Object as MemberExpression).Member.Name; string Result = string.Format("{0} like {1}", Name, Value); return Result; } private static string Len(MethodCallExpression expression, object value, ExpressionType expressiontype) { object Name = (expression.Arguments[0] as MemberExpression).Member.Name; string Operator = GetOperator(expressiontype); string Result = string.Format("len({0}){1}{2}", Name, Operator, value.ToString()); return Result; } }

/// <summary> /// 面向切面的緩存使用 /// </summary> public class RedisCacheAOP : CacheAOPbase { //通過注入的方式,把緩存操作接口通過構造函數注入 private readonly IRedisCacheManager _cache; public RedisCacheAOP(IRedisCacheManager cache) { _cache = cache; } //Intercept方法是攔截的關鍵所在,也是IInterceptor接口中的唯一定義 //代碼已經合並 ,學習pr流程 public override void Intercept(IInvocation invocation) { var method = invocation.MethodInvocationTarget ?? invocation.Method; if (method.ReturnType == typeof(void) || method.ReturnType == typeof(Task)) { invocation.Proceed(); return; } //對當前方法的特性驗證 if (method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) is CachingAttribute qCachingAttribute) { //獲取自定義緩存鍵 var cacheKey = qCachingAttribute.CustomKeyValue ?? CustomCacheKey(invocation); //注意是 string 類型,方法GetValue var cacheValue = _cache.GetValue(cacheKey); if (cacheValue != null) { if (qCachingAttribute.IsDelete) { //刪除Redis里面的數據 _cache.Remove(cacheKey); } else { //將當前獲取到的緩存值,賦值給當前執行方法 Type returnType; if (typeof(Task).IsAssignableFrom(method.ReturnType)) { returnType = method.ReturnType.GenericTypeArguments.FirstOrDefault(); } else { returnType = method.ReturnType; } dynamic _result = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, returnType); invocation.ReturnValue = (typeof(Task).IsAssignableFrom(method.ReturnType)) ? Task.FromResult(_result) : _result; return; } } //去執行當前的方法 invocation.Proceed(); //存入緩存 if (!string.IsNullOrWhiteSpace(cacheKey) && qCachingAttribute.IsDelete == false) { object response; //Type type = invocation.ReturnValue?.GetType(); var type = invocation.Method.ReturnType; if (typeof(Task).IsAssignableFrom(type)) { var resultProperty = type.GetProperty("Result"); response = resultProperty.GetValue(invocation.ReturnValue); } else { response = invocation.ReturnValue; } if (response == null) response = string.Empty; _cache.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration)); } } else { invocation.Proceed();//直接執行被攔截方法 } } }
注入到服務到 Autofac
Setup的AutofacModuleRegister文件注入redisaop緩存
protected override void Load(ContainerBuilder builder) { //注冊RedisAop builder.RegisterType<RedisCacheAOP>(); //注冊Service var assemblysServices = Assembly.Load("Webapi.Core.Service"); builder.RegisterAssemblyTypes(assemblysServices) .InstancePerDependency()//瞬時單例 .AsImplementedInterfaces()////自動以其實現的所有接口類型暴露(包括IDisposable接口) .EnableInterfaceInterceptors() //引用Autofac.Extras.DynamicProxy; .InterceptedBy(typeof(RedisCacheAOP));//可以放一個AOP攔截器集合 //注冊Repository var assemblysRepository = Assembly.Load("Webapi.Core.Repository"); builder.RegisterAssemblyTypes(assemblysRepository) .InstancePerDependency()//瞬時單例 .AsImplementedInterfaces()////自動以其實現的所有接口類型暴露(包括IDisposable接口) .EnableInterfaceInterceptors(); //引用Autofac.Extras.DynamicProxy; }
測試APO緩存
User控制器新建AopTest接口
/// <summary> /// 測試aop緩存redis /// </summary> /// <returns></returns> [HttpGet] public async Task<IActionResult> AopTest(int id) { var sucess = await _userService.GetUserDetails(id); return Ok(sucess); }
userService的GetUserDetails方法上方標注特性
[Caching(AbsoluteExpiration =1)] public async Task<UserViewModel> GetUserDetails(int id) { var userinfo = await userDal.GetById(id); if (userinfo != null) { //UserViewModel model = new UserViewModel() //{ // UserId = userinfo.UserId, // UserName = userinfo.UserName, // Address = "北京市xx區xx小區", // Age = userinfo.Age, // Birthday = "1996-06-26", // Phone = "13888888888" //}; UserViewModel model = iMapper.Map<UserViewModel>(userinfo); model.Address = "北京市xx區xx小區"; model.Birthday = "1996-06-26"; model.Phone = "13888888888"; return model; } else { return null; } }
斷點達到的時候可以看到沒有數據
再次執行Aoptest接口,可以看到數據已經被緩存了。這樣我們就實現了基於AOP的緩存