Пример #1
0
        public void AddCacheControl(CacheControlPayload payload)
        {
            if (payload.MaxAge == -1)
            {
                // ignore unknow cache
                return;
            }

            if (MaxAge == -1)
            {
                MaxAge = payload.MaxAge;
            }
            else
            {
                MaxAge = Math.Min(MaxAge, payload.MaxAge);
            }
        }
        public async Task <object> Resolve(
            ResolveFieldContext context,
            FieldMiddlewareDelegate next)
        {
            var _cachePolicy = (ICachePolicy)((IServiceProvider)context.UserContext["serviceProvider"]).GetService(typeof(ICachePolicy));

            CacheControlPayload cacheControlPayload = null;

            // check if the field contains cache attribute
            object tempValue;

            if (context.FieldDefinition.Metadata.TryGetValue(CacheControlAttribute.MetadataKey, out tempValue))
            {
                cacheControlPayload = tempValue as CacheControlPayload;
            }
            // check if the field type has cache attriubute
            else
            {
                IGraphType returnType;
                if (context.ReturnType is ListGraphType list)
                {
                    returnType = list.ResolvedType;
                }
                else
                {
                    returnType = context.ReturnType;
                }
                if (returnType.Metadata.TryGetValue(CacheControlAttribute.MetadataKey, out tempValue))
                {
                    cacheControlPayload = tempValue as CacheControlPayload;
                }
            }

            if (cacheControlPayload == null)
            {
                // if the cache is not assigned by the user use the default
                cacheControlPayload = new CacheControlPayload(-1, CacheControlScope.Public);
            }

            // add the cache payload to the cache policy
            _cachePolicy.AddCacheControl(cacheControlPayload);

            return(await next(context));
        }