public override async Task Invoke(AspectContext context, AspectDelegate next) { var currentCacheKey = Key.FillValue(context.GetParamsDictionary(), context.GetDefaultKey()); //返回值类型 var returnType = context.GetReturnType(); //从缓存取值 var cacheValue = GetCahceValue(currentCacheKey, returnType, context); if (cacheValue != null) { return; } //不加锁,直接返回 if (!ThreadLock) { await GetDirectValueWithSetCache(context, next, currentCacheKey, returnType); return; } using (await _lock.LockAsync()) { cacheValue = GetCahceValue(currentCacheKey, returnType, context); if (cacheValue != null) { return; } await GetDirectValueWithSetCache(context, next, currentCacheKey, returnType); } }
public async override Task Invoke(AspectContext context, AspectDelegate next) { if (!IsOpen || Seconds <= 0) { await next(context); return; } try { var cacheService = context.ServiceProvider.GetService <ICacheService>(); Type returnType = context.GetReturnType(); string key = Utils.GetParamterKey(Prefix, Key, ParamterKey, context.Parameters); var result = cacheService.Get(key, returnType); if (result == null) { await next(context); var value = await context.GetReturnValue(); if (value != null) { cacheService.Set(key, TimeSpan.FromSeconds(Seconds), value); } } else { context.ReturnValue = context.ResultFactory(result, returnType, context.IsAsync()); } } catch (Exception ex) { var logger = context.ServiceProvider.GetService <ILogger <CacheMethodAttribute> >(); logger.LogError($"CacheMethod:Key:{Key},ParamterKey:{ParamterKey} {ex.FormatMessage()}"); await next(context); } }
public async override Task Invoke(AspectContext context, AspectDelegate next) { if (!IsOpen) { await next(context); return; } try { var cacheService = context.ServiceProvider.GetService <ICacheService>(); Type returnType = context.GetReturnType(); string key = Utils.GetParamterKey(Prefix, Key, ParamterKey, context.Parameters); var result = cacheService.Get(key, returnType); if (result == null) { await Next(context, next, cacheService, key); } else { context.ReturnValue = context.ResultFactory(result, returnType, context.IsAsync()); } } catch (Exception ex) { var logger = context.ServiceProvider.GetService <ILogger <CacheTiggerAttribute> >(); logger.LogError($"CacheInterceptor:Key:{Key},ParamterKey:{ParamterKey} {ex.FormatMessage()}"); await next(context); } }
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); } } }
public async override Task Invoke(AspectContext context, AspectDelegate next) { if (!IsOpen) { await next(context); return; } try { var redis = context.ServiceProvider.GetService <IRedisService>(); var option = context.ServiceProvider.GetService <IOptions <CacheOptions> >(); Type returnType = context.GetReturnType(); string key = Utils.GetParamterKey("", "", Key, context.Parameters); var result = redis.HashGet <string>(HashKey, key, option.Value.RedisRead, RedisSerializerOptions.RedisValue); if (result.IsEmpty()) { await next(context); var value = await context.GetReturnValue(); if (value != null) { var json = JsonConvert.SerializeObject(value); if (Seconds > 0) { bool exists = redis.KeyExists(HashKey, option.Value.RedisRead); redis.HashSet(HashKey, key, json, option.Value.RedisWrite, RedisSerializerOptions.RedisValue); if (!exists) { redis.KeyExpire(HashKey, Seconds, option.Value.RedisWrite); } } else { redis.HashSet(HashKey, key, json, option.Value.RedisWrite, RedisSerializerOptions.RedisValue); } } } else { var value = JsonConvert.DeserializeObject(result, returnType); context.ReturnValue = context.ResultFactory(value, returnType, context.IsAsync()); } } catch (Exception ex) { var logger = context.ServiceProvider.GetService <ILogger <RedisCacheMethodAttribute> >(); logger.LogError($"RedisCacheMethod:Key:{Key} {ex.FormatMessage()}"); await next(context); } }