public async override Task Invoke(AspectContext context, AspectDelegate next) { CacheAbleAttribute attribute = context.GetAttribute <CacheAbleAttribute>(); if (attribute == null) { await context.Invoke(next); return; } try { Database = RedisClient.GetDatabase(); string cacheKey = KeyGenerator.GetCacheKey(context.ServiceMethod, context.Parameters, attribute.CacheKeyPrefix); string cacheValue = await GetCacheAsync(cacheKey); Type returnType = context.GetReturnType(); if (string.IsNullOrWhiteSpace(cacheValue)) { if (attribute.OnceUpdate) { string lockKey = $"Lock_{cacheKey}"; RedisValue token = Environment.MachineName; if (await Database.LockTakeAsync(lockKey, token, TimeSpan.FromSeconds(10))) { try { var result = await RunAndGetReturn(context, next); await SetCache(cacheKey, result, attribute.Expiration); return; } finally { await Database.LockReleaseAsync(lockKey, token); } } else { for (int i = 0; i < 5; i++) { Thread.Sleep(i * 100 + 500); cacheValue = await GetCacheAsync(cacheKey); if (!string.IsNullOrWhiteSpace(cacheValue)) { break; } } if (string.IsNullOrWhiteSpace(cacheValue)) { var defaultValue = CreateDefaultResult(returnType); context.ReturnValue = ResultFactory(defaultValue, returnType, context.IsAsync()); return; } } } else { var result = await RunAndGetReturn(context, next); await SetCache(cacheKey, result, attribute.Expiration); return; } } var objValue = await DeserializeCache(cacheKey, cacheValue, returnType); //缓存值不可用 if (objValue == null) { await context.Invoke(next); return; } context.ReturnValue = ResultFactory(objValue, returnType, context.IsAsync()); } catch (Exception) { if (context.ReturnValue == null) { await context.Invoke(next); } } }