Esempio n. 1
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var cachedConfigurationProvider = context.ServiceProvider.GetService <IOptionsMonitor <FilterCachedConfiguration> >();
            var configCache = context.GetCacheConfigurationByMethodName(cachedConfigurationProvider.CurrentValue);

            var methodReturnType = context.ProxyMethod.ReturnType;

            if (
                configCache == null ||
                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(configCache.TimeToLiveSeconds ?? TimeToLiveSeconds)
                );
        }