private async Task Next(AspectContext context, AspectDelegate next, ICacheService cacheService, string key) { var scheduler = context.ServiceProvider.GetService <QuartzService>(); if (!scheduler.CacheContainer.ContainsKey(key)) { scheduler.CacheContainer.TryAdd(key, async() => { await next(context); var value = await context.GetReturnValue(); if (value != null) { cacheService.Set(key, value); } }); scheduler.JoinJobAsync(key, TimeSpan.FromSeconds(Seconds)).Wait(); } await next(context); var value = await context.GetReturnValue(); if (value != null) { cacheService.Set(key, value); } }
/// <summary> /// 直接调用方法,并把结果加入缓存 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <param name="key">缓存key</param> /// <param name="type">缓存值类型</param> /// <returns></returns> public async Task GetDirectValueWithSetCache(AspectContext context, AspectDelegate next, string key, Type type) { //执行方法 await next(context); //获取缓存过期时间 var limitTime = GetCacheNewTime(Type, Length); var value = await context.GetReturnValue(); //加入缓存 CacheProvider.Set(key, value, type, limitTime); }
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 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); } }