예제 #1
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            if (context.HasAttributeType(GetType()))
            {
                await next(context);

                return;
            }

            await next(context);

            ResponseCacheService = context.ServiceProvider.GetService <IResponseCacheService>();
            await ResponseCacheService
            .RemoveAllByPatternAsync(RemovePattern);
        }
예제 #2
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var methodReturnType = context.ProxyMethod.ReturnType;

            if (
                context.HasAttributeType(GetType()) ||
                methodReturnType == typeof(void) ||
                methodReturnType == typeof(Task) ||
                methodReturnType == typeof(ValueTask)
                )
            {
                await next(context);

                return;
            }

            if (string.IsNullOrWhiteSpace(CacheName))
            {
                CacheName = context.GetGenerateKeyByMethodNameAndValues();
            }

            ResponseCacheService = context.ServiceProvider.GetService <IResponseCacheService>();

            var returnType = context.IsAsync() ? methodReturnType.GenericTypeArguments.FirstOrDefault() : methodReturnType;

            var cachedValue = await ResponseCacheService.GetCachedResponseAsync(CacheName, returnType);

            if (cachedValue != null)
            {
                context.SetReturnType(methodReturnType, cachedValue);
                return;
            }

            await next(context);

            await ResponseCacheService
            .SetCacheResponseAsync(
                CacheName,
                await context.GetReturnValueAsync(),
                TimeSpan.FromSeconds(TimeToLiveSeconds)
                );
        }