Пример #1
0
        public OutputCacheEntry CreateOutputCacheEntry(CachedRawResponse cachedRawResponse, string depKey, string[] fileDependencies)
        {
            //
            // Do the converting from FX internal IHttpResponseElement classes to public ResponseElement classes
            List <ResponseElement> responseElements = new List <ResponseElement>();

            foreach (var buffer in cachedRawResponse.RawResponse.Buffers)
            {
                Type            type = buffer.GetType();
                ResponseElement elem = null;
                //
                // HttpFileResponseElement
                if (type == HttpFileResponseElementType)
                {
                    elem = CreateFileResponseElement(buffer);
                }
                //
                // HttpSubstBlockResponseElement
                else if (type == HttpSubstBlockResponseElementType)
                {
                    elem = CreateSubstBlockResponseElement(buffer);
                }
                //
                // IHttpResponseElement
                else
                {
                    elem = CreateMemoryResponseElement(buffer);
                }
                if (elem != null)
                {
                    responseElements.Add(elem);
                }
            }

            return(new OutputCacheEntry()
            {
                CachedVaryId = cachedRawResponse.CachedVaryId,
                Settings = cachedRawResponse.CachePolicy,
                KernelCacheUrl = cachedRawResponse.KernelCacheUrl,
                DependenciesKey = depKey,
                Dependencies = fileDependencies,
                StatusCode = cachedRawResponse.RawResponse.StatusCode,
                StatusDescription = cachedRawResponse.RawResponse.StatusDescription,
                HeaderElements = cachedRawResponse.RawResponse.Headers,
                ResponseBuffers = responseElements
            });
        }
        private async Task OnEnterAsync(object source, EventArgs eventArgs)
        {
            var app    = (HttpApplication)source;
            var helper = new OutputCacheHelper(new HttpContextWrapper(app.Context));

            if (!helper.IsHttpMethodSupported())
            {
                return;
            }

            // Create a lookup key. Also store the key in global parameter _key to be used inside OnLeave() later
            string key = helper.CreateOutputCachedItemKey(null);

            // Lookup the cache vary using the key
            object item = await helper.GetAsync(key);

            if (item == null)
            {
                return;
            }

            // 'item' may be one of the following:
            //  - a CachedVary object (if the object varies by something)
            //  - a CachedRawResponse object (i.e. it doesn't vary on anything)
            //  First assume it's a CacheVary and try to get the cachedItem with it
            CachedRawResponse cachedRawResponse = null;
            var cachedVary = item as CachedVary;

            if (cachedVary != null)
            {
                var cachedItem = await helper.GetAsCacheVaryAsync(cachedVary);

                if (cachedItem != null)
                {
                    cachedRawResponse = (CachedRawResponse)cachedItem;
                }
            }
            if (cachedRawResponse == null)
            {
                cachedRawResponse = item as CachedRawResponse;
            }
            if (cachedRawResponse == null)
            {
                return;
            }

            // From this point on, we have an Raw Response entry to work with.
            HttpCachePolicySettings settings = cachedRawResponse.CachePolicy;

            if (helper.CheckCachedVary(cachedVary, settings))
            {
                return;
            }
            if (settings.IgnoreRangeRequests && helper.IsRangeRequest())
            {
                return;
            }
            if (helper.CheckHeaders(settings))
            {
                return;
            }
            if (await helper.CheckValidityAsync(key, settings))
            {
                return;
            }
            if (!helper.IsContentEncodingAcceptable(cachedVary, cachedRawResponse.RawResponse))
            {
                return;
            }
            helper.UpdateCachedResponse(settings, cachedRawResponse.RawResponse);

            //Re-insert entry in kernel cache if necessary
            if (helper.IsKernelCacheAPISupported() && cachedRawResponse.KernelCacheUrl != null)
            {
                OutputCacheUtility.SetupKernelCaching(cachedRawResponse.KernelCacheUrl, app.Context.Response);
            }
            //Complete request
            app.CompleteRequest();
        }