public override bool Equals(object obj) { Cachekey another = obj as Cachekey; if (null == another) { return(false); } if (!this.Method.Equals(another.Method)) { return(false); } for (int index = 0; index < this.InputArguments.Length; index++) { var argument1 = this.InputArguments[index]; var argument2 = another.InputArguments[index]; if (argument1 == null && argument2 == null) { continue; } if (argument1 == null || argument2 == null) { return(false); } if (!argument2.Equals(argument2)) { return(false); } } return(true); }
/// <summary> /// 动态拦截的方法 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async override Task Invoke(AspectContext context, AspectDelegate next) { var Key = new Cachekey(context.ServiceMethod, context.Parameters).GetHashCode(); if (memoryCache.TryGetValue(Key, out object value)) { context.ReturnValue = ((MemoryCacheEntity)value).ReturnValue; var Parameters = ((MemoryCacheEntity)value).Parameters; if (context.Parameters != null && Parameters != null && Parameters.Length == context.Parameters.Length) { for (int i = 0; i < context.Parameters.Length; i++) { context.Parameters[i] = Parameters[i]; } } } else { await context.Invoke(next); if (CachingTime <= 0) { //不过期 memoryCache.Set(Key, new MemoryCacheEntity(context.ReturnValue, context.Parameters)); } else { memoryCache.Set(Key, new MemoryCacheEntity(context.ReturnValue, context.Parameters), new TimeSpan(0, 0, CachingTime)); } } }
public async Task InvokeAsync(InvocationContext context, IMemoryCache cache, IOptions<MemoryCacheEntryOptions> optionsAccessor) { var key = new Cachekey(context.Method, context.Arguments); if (cache.TryGetValue(key, out object value)) { context.ReturnValue = value; } else { await context.ProceedAsync(); cache.Set(key, context.ReturnValue, optionsAccessor.Value); } }
public async Task InvokeAsync(InvocationContext context) { var key = new Cachekey(context.Method, context.Arguments); if (_cache.TryGetValue(key, out object value)) { context.ReturnValue = value; } else { await _next(context); _cache.Set(key, context.ReturnValue, _options); } }
public async Task InvokeAsync(InvocationContext context) { if (!context.Method.GetParameters().All(it => it.IsIn)) { await _next(context); } var key = new Cachekey(context.Method, context.Arguments); if (_cache.TryGetValue(key, out object value)) { context.ReturnValue = value; } else { await _next(context); _cache.Set(key, context.ReturnValue, _options); } }
public override bool Equals(object obj) { Cachekey another = obj as Cachekey; if (null == another) { return(false); } if (!this.Method.Equals(another.Method)) { return(false); } for (int index = 0; index < this.InputArguments.Length; index++) { if (!this.InputArguments[index].Equals(another.InputArguments[index])) { return(false); } } return(true); }