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) { 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); } }