Пример #1
0
        // insert cached vary or output cache entry
        internal static void InsertResponse(String cachedVaryKey, CachedVary cachedVary,
                                            String rawResponseKey, CachedRawResponse rawResponse,
                                            CacheDependency dependencies,
                                            DateTime absExp, TimeSpan slidingExp)
        {
            // if the provider is undefined or the fragment can't be inserted in the
            // provider, insert it in the internal cache.
            OutputCacheProvider provider = GetProvider(HttpContext.Current);

            //
            // CachedVary can be serialized.
            // CachedRawResponse is not always serializable.
            //

            bool useProvider = (provider != null);

            if (useProvider)
            {
                bool canUseProvider = (IsSubstBlockSerializable(rawResponse._rawResponse) &&
                                       rawResponse._settings.IsValidationCallbackSerializable() &&
                                       slidingExp == Cache.NoSlidingExpiration &&
                                       (dependencies == null || dependencies.IsFileDependency()));

                if (useProvider && !canUseProvider)
                {
                    throw new ProviderException(SR.GetString(SR.Provider_does_not_support_policy_for_responses, provider.Name));
                }
            }

#if DBG
            bool cachedVaryPutInCache = (cachedVary != null);
#endif
            if (cachedVary != null)
            {
                /*
                 * Add the CachedVary item so that a request will know
                 * which headers are needed to issue another request.
                 *
                 * Use the Add method so that we guarantee we only use
                 * a single CachedVary and don't overwrite existing ones.
                 */

                CachedVary cachedVaryInCache;
                if (!useProvider)
                {
                    cachedVaryInCache = OutputCache.UtcAdd(cachedVaryKey, cachedVary);
                }
                else
                {
                    cachedVaryInCache = (CachedVary)provider.Add(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                }

                if (cachedVaryInCache != null)
                {
                    if (!cachedVary.Equals(cachedVaryInCache))
                    {
                        if (!useProvider)
                        {
                            HttpRuntime.CacheInternal.UtcInsert(cachedVaryKey, cachedVary);
                        }
                        else
                        {
                            provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                        }
                    }
                    else
                    {
                        cachedVary = cachedVaryInCache;
#if DBG
                        cachedVaryPutInCache = false;
#endif
                    }
                }

                if (!useProvider)
                {
                    AddCacheKeyToDependencies(ref dependencies, cachedVaryKey);
                }

                // not all caches support cache key dependencies, but we can use a "change number" to associate
                // the ControlCachedVary and the PartialCachingCacheEntry
                rawResponse._cachedVaryId = cachedVary.CachedVaryId;
            }

            // Now insert into the cache (use cache provider if possible, otherwise use internal cache)
            if (!useProvider)
            {
                HttpRuntime.CacheInternal.UtcInsert(rawResponseKey, rawResponse,
                                                    dependencies,
                                                    absExp, slidingExp,
                                                    CacheItemPriority.Normal,
                                                    s_entryRemovedCallback);

                IncrementCount();

                PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_ENTRIES);
                PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_TURNOVER_RATE);
            }
            else
            {
                string   depKey   = null;
                string[] fileDeps = null;
                if (dependencies != null)
                {
                    depKey   = OUTPUTCACHE_KEYPREFIX_DEPENDENCIES + dependencies.GetUniqueID();
                    fileDeps = dependencies.GetFileDependencies();
                }
                OutputCacheEntry oce = Convert(rawResponse, depKey, fileDeps);
                provider.Set(rawResponseKey, oce, absExp);
                if (dependencies != null)
                {
                    // use Add and dispose dependencies if there's already one in the cache
                    Object d = HttpRuntime.CacheInternal.UtcAdd(depKey, new DependencyCacheEntry(rawResponseKey, oce.KernelCacheUrl, provider.Name),
                                                                dependencies,
                                                                absExp, Cache.NoSlidingExpiration,
                                                                CacheItemPriority.Normal, s_dependencyRemovedCallback);
                    if (d != null)
                    {
                        dependencies.Dispose();
                    }
                }
            }
#if DBG
            string cachedVaryType = (cachedVaryPutInCache) ? "CachedVary" : "";
            string providerUsed   = (useProvider) ? provider.Name : "CacheInternal";
            Debug.Trace("OutputCache", "InsertResposne("
                        + cachedVaryKey + ", "
                        + cachedVaryType + ", "
                        + rawResponseKey + ", CachedRawResponse, ...) -->"
                        + providerUsed);
#endif
        }
Пример #2
0
        // insert fragment
        internal static void InsertFragment(String cachedVaryKey, ControlCachedVary cachedVary,
                                            String fragmentKey, PartialCachingCacheEntry fragment,
                                            CacheDependency dependencies,
                                            DateTime absExp, TimeSpan slidingExp,
                                            String providerName)
        {
            // if providerName is not null, find the provider in the collection.
            // if providerName is null, use default provider.
            // if the default provider is undefined or the fragment can't be inserted in the
            // provider, insert it in the internal cache.
            OutputCacheProvider provider = GetFragmentProvider(providerName);

            //
            // ControlCachedVary and PartialCachingCacheEntry can be serialized
            //

            bool useProvider = (provider != null);

            if (useProvider)
            {
                bool canUseProvider = (slidingExp == Cache.NoSlidingExpiration &&
                                       (dependencies == null || dependencies.IsFileDependency()));

                if (useProvider && !canUseProvider)
                {
                    throw new ProviderException(SR.GetString(SR.Provider_does_not_support_policy_for_fragments, providerName));
                }
            }

#if DBG
            bool cachedVaryPutInCache = (cachedVary != null);
#endif
            if (cachedVary != null)
            {
                // Add the ControlCachedVary item so that a request will know
                // which varies are needed to issue another request.

                // Use the Add method so that we guarantee we only use
                // a single ControlCachedVary and don't overwrite existing ones.
                ControlCachedVary cachedVaryInCache;
                if (!useProvider)
                {
                    cachedVaryInCache = OutputCache.UtcAdd(cachedVaryKey, cachedVary);
                }
                else
                {
                    cachedVaryInCache = (ControlCachedVary)provider.Add(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                }

                if (cachedVaryInCache != null)
                {
                    if (!cachedVary.Equals(cachedVaryInCache))
                    {
                        // overwrite existing cached vary
                        if (!useProvider)
                        {
                            HttpRuntime.CacheInternal.UtcInsert(cachedVaryKey, cachedVary);
                        }
                        else
                        {
                            provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                        }
                    }
                    else
                    {
                        cachedVary = cachedVaryInCache;
#if DBG
                        cachedVaryPutInCache = false;
#endif
                    }
                }

                if (!useProvider)
                {
                    AddCacheKeyToDependencies(ref dependencies, cachedVaryKey);
                }

                // not all caches support cache key dependencies, but we can use a "change number" to associate
                // the ControlCachedVary and the PartialCachingCacheEntry
                fragment._cachedVaryId = cachedVary.CachedVaryId;
            }

            // Now insert into the cache (use cache provider if possible, otherwise use internal cache)
            if (!useProvider)
            {
                HttpRuntime.CacheInternal.UtcInsert(fragmentKey, fragment,
                                                    dependencies,
                                                    absExp, slidingExp,
                                                    CacheItemPriority.Normal,
                                                    null);
            }
            else
            {
                string depKey = null;
                if (dependencies != null)
                {
                    depKey = OUTPUTCACHE_KEYPREFIX_DEPENDENCIES + dependencies.GetUniqueID();
                    fragment._dependenciesKey = depKey;
                    fragment._dependencies    = dependencies.GetFileDependencies();
                }
                provider.Set(fragmentKey, fragment, absExp);
                if (dependencies != null)
                {
                    // use Add and dispose dependencies if there's already one in the cache
                    Object d = HttpRuntime.CacheInternal.UtcAdd(depKey, new DependencyCacheEntry(fragmentKey, null, provider.Name),
                                                                dependencies,
                                                                absExp, Cache.NoSlidingExpiration,
                                                                CacheItemPriority.Normal, s_dependencyRemovedCallbackForFragment);
                    if (d != null)
                    {
                        dependencies.Dispose();
                    }
                }
            }

#if DBG
            string cachedVaryType = (cachedVaryPutInCache) ? "ControlCachedVary" : "";
            string providerUsed   = (useProvider) ? provider.Name : "CacheInternal";
            Debug.Trace("OutputCache", "InsertFragment("
                        + cachedVaryKey + ", "
                        + cachedVaryType + ", "
                        + fragmentKey + ", PartialCachingCacheEntry, ...) -->"
                        + providerUsed);
#endif
        }
Пример #3
0
        internal static void InsertFragment(string cachedVaryKey, ControlCachedVary cachedVary, string fragmentKey, PartialCachingCacheEntry fragment, CacheDependency dependencies, DateTime absExp, TimeSpan slidingExp, string providerName)
        {
            OutputCacheProvider fragmentProvider = GetFragmentProvider(providerName);
            bool flag = fragmentProvider != null;

            if (flag)
            {
                bool flag2 = (slidingExp == Cache.NoSlidingExpiration) && ((dependencies == null) || dependencies.IsFileDependency());
                if (flag && !flag2)
                {
                    throw new ProviderException(System.Web.SR.GetString("Provider_does_not_support_policy_for_fragments", new object[] { providerName }));
                }
            }
            if (cachedVary != null)
            {
                ControlCachedVary vary;
                if (!flag)
                {
                    vary = UtcAdd(cachedVaryKey, cachedVary);
                }
                else
                {
                    vary = (ControlCachedVary)fragmentProvider.Add(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                }
                if (vary != null)
                {
                    if (!cachedVary.Equals(vary))
                    {
                        if (!flag)
                        {
                            HttpRuntime.CacheInternal.UtcInsert(cachedVaryKey, cachedVary);
                        }
                        else
                        {
                            fragmentProvider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                        }
                    }
                    else
                    {
                        cachedVary = vary;
                    }
                }
                if (!flag)
                {
                    AddCacheKeyToDependencies(ref dependencies, cachedVaryKey);
                }
                fragment._cachedVaryId = cachedVary.CachedVaryId;
            }
            if (!flag)
            {
                HttpRuntime.CacheInternal.UtcInsert(fragmentKey, fragment, dependencies, absExp, slidingExp, CacheItemPriority.Normal, null);
            }
            else
            {
                string key = null;
                if (dependencies != null)
                {
                    key = "aD" + dependencies.GetUniqueID();
                    fragment._dependenciesKey = key;
                    fragment._dependencies    = dependencies.GetFileDependencies();
                }
                fragmentProvider.Set(fragmentKey, fragment, absExp);
                if ((dependencies != null) && (HttpRuntime.CacheInternal.UtcAdd(key, new DependencyCacheEntry(fragmentKey, null, fragmentProvider.Name), dependencies, absExp, Cache.NoSlidingExpiration, CacheItemPriority.Normal, s_dependencyRemovedCallbackForFragment) != null))
                {
                    dependencies.Dispose();
                }
            }
        }
Пример #4
0
        internal static void InsertResponse(string cachedVaryKey, CachedVary cachedVary, string rawResponseKey, CachedRawResponse rawResponse, CacheDependency dependencies, DateTime absExp, TimeSpan slidingExp)
        {
            OutputCacheProvider provider = GetProvider(HttpContext.Current);
            bool flag = provider != null;

            if (flag)
            {
                bool flag2 = ((IsSubstBlockSerializable(rawResponse._rawResponse) && rawResponse._settings.IsValidationCallbackSerializable()) && (slidingExp == Cache.NoSlidingExpiration)) && ((dependencies == null) || dependencies.IsFileDependency());
                if (flag && !flag2)
                {
                    throw new ProviderException(System.Web.SR.GetString("Provider_does_not_support_policy_for_responses", new object[] { provider.Name }));
                }
            }
            if (cachedVary != null)
            {
                CachedVary vary;
                if (!flag)
                {
                    vary = UtcAdd(cachedVaryKey, cachedVary);
                }
                else
                {
                    vary = (CachedVary)provider.Add(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                }
                if (vary != null)
                {
                    if (!cachedVary.Equals(vary))
                    {
                        if (!flag)
                        {
                            HttpRuntime.CacheInternal.UtcInsert(cachedVaryKey, cachedVary);
                        }
                        else
                        {
                            provider.Set(cachedVaryKey, cachedVary, Cache.NoAbsoluteExpiration);
                        }
                    }
                    else
                    {
                        cachedVary = vary;
                    }
                }
                if (!flag)
                {
                    AddCacheKeyToDependencies(ref dependencies, cachedVaryKey);
                }
                rawResponse._cachedVaryId = cachedVary.CachedVaryId;
            }
            if (!flag)
            {
                HttpRuntime.CacheInternal.UtcInsert(rawResponseKey, rawResponse, dependencies, absExp, slidingExp, CacheItemPriority.Normal, s_entryRemovedCallback);
                IncrementCount();
                PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_ENTRIES);
                PerfCounters.IncrementCounter(AppPerfCounter.OUTPUT_CACHE_TURNOVER_RATE);
            }
            else
            {
                string   depKey           = null;
                string[] fileDependencies = null;
                if (dependencies != null)
                {
                    depKey           = "aD" + dependencies.GetUniqueID();
                    fileDependencies = dependencies.GetFileDependencies();
                }
                OutputCacheEntry entry = Convert(rawResponse, depKey, fileDependencies);
                provider.Set(rawResponseKey, entry, absExp);
                if ((dependencies != null) && (HttpRuntime.CacheInternal.UtcAdd(depKey, new DependencyCacheEntry(rawResponseKey, entry.KernelCacheUrl, provider.Name), dependencies, absExp, Cache.NoSlidingExpiration, CacheItemPriority.Normal, s_dependencyRemovedCallback) != null))
                {
                    dependencies.Dispose();
                }
            }
        }
Пример #5
0
        void DoCacheInsert(HttpContext context, HttpApplication app, HttpResponse response)
        {
            string vary_key = context.Request.FilePath;
            string key;
            OutputCacheProvider provider = FindCacheProvider(app);
            CachedVaryBy        varyby = provider.Get(vary_key) as CachedVaryBy;
            CachedRawResponse   prev = null;
            bool            lookup = true;
            string          cacheKey = null, cacheValue = null;
            HttpCachePolicy cachePolicy = response.Cache;

            if (varyby == null)
            {
                varyby = new CachedVaryBy(cachePolicy, vary_key);
                provider.Add(vary_key, varyby, Cache.NoAbsoluteExpiration);
                lookup   = false;
                cacheKey = vary_key;
            }

            key = varyby.CreateKey(vary_key, context);

            if (lookup)
            {
                prev = provider.Get(key) as CachedRawResponse;
            }

            if (prev == null)
            {
                CachedRawResponse c = response.GetCachedResponse();
                if (c != null)
                {
                    string [] keys = new string [] { vary_key };
                    DateTime  utcExpiry, absoluteExpiration;
                    TimeSpan  slidingExpiration;

                    c.VaryBy = varyby;
                    varyby.ItemList.Add(key);

                    if (cachePolicy.Sliding)
                    {
                        slidingExpiration  = TimeSpan.FromSeconds(cachePolicy.Duration);
                        absoluteExpiration = Cache.NoAbsoluteExpiration;
                        utcExpiry          = DateTime.UtcNow + slidingExpiration;
                    }
                    else
                    {
                        slidingExpiration  = Cache.NoSlidingExpiration;
                        absoluteExpiration = cachePolicy.Expires;
                        utcExpiry          = absoluteExpiration.ToUniversalTime();
                    }

                    provider.Set(key, c, utcExpiry);
                    HttpRuntime.InternalCache.Insert(key, c, new CacheDependency(null, keys), absoluteExpiration, slidingExpiration,
                                                     CacheItemPriority.Normal, response_removed);
                    cacheValue = key;
                }
            }

            if (cacheKey != null)
            {
                lock (keysCacheLock) {
                    if (keysCache == null)
                    {
                        BuildManager.RemoveEntry += new BuildManagerRemoveEntryEventHandler(OnBuildManagerRemoveEntry);
                        keysCache = new Dictionary <string, string> (StringComparer.Ordinal);
                        keysCache.Add(cacheKey, cacheValue);
                    }
                    else if (!keysCache.ContainsKey(cacheKey))
                    {
                        keysCache.Add(cacheKey, cacheValue);
                    }
                }
            }
        }