Exemple #1
0
        private static OutputCacheProvider GetProvider(HttpContext context)
        {
            Debug.Assert(context != null, "context != null");
            if (context == null)
            {
                return(null);
            }
            // Call GetOutputCacheProviderName
            // so it can determine which provider to use.
            HttpApplication app  = context.ApplicationInstance;
            string          name = app.GetOutputCacheProviderName(context);

            if (name == null)
            {
                throw new ProviderException(SR.GetString(SR.GetOutputCacheProviderName_Invalid, name));
            }
            // AspNetInternalProvider means use the internal cache
            if (name == OutputCache.ASPNET_INTERNAL_PROVIDER_NAME)
            {
                return(null);
            }
            OutputCacheProvider provider = (s_providers == null) ? null : s_providers[name];

            if (provider == null)
            {
                throw new ProviderException(SR.GetString(SR.GetOutputCacheProviderName_Invalid, name));
            }
            return(provider);
        }
Exemple #2
0
		OutputCacheProvider FindCacheProvider (HttpApplication app)
		{				
#if NET_4_0
			HttpContext ctx = HttpContext.Current;
			if (app == null) {
				app = ctx != null ? ctx.ApplicationInstance : null;

				if (app == null)
					throw new InvalidOperationException ("Unable to find output cache provider.");
			}

			string providerName = app.GetOutputCacheProviderName (ctx);
			if (String.IsNullOrEmpty (providerName))
				throw new ProviderException ("Invalid OutputCacheProvider name. Name must not be null or an empty string.");
			
			if (String.Compare (providerName, OutputCache.DEFAULT_PROVIDER_NAME, StringComparison.Ordinal) == 0) {
				if (provider == null)
					provider = new InMemoryOutputCacheProvider ();
				return provider;
			}

			OutputCacheProviderCollection providers = OutputCache.Providers;
			OutputCacheProvider ret = providers != null ? providers [providerName] : null;

			if (ret == null)
				throw new ProviderException (String.Format ("OutputCacheProvider named '{0}' cannot be found.", providerName));

			return ret;
#else
			if (provider == null)
				provider = new InMemoryOutputCacheProvider ();
			
			return provider;
#endif
		}
Exemple #3
0
        // remove cache vary
        // remove entry
        internal static void Remove(String key, HttpContext context)
        {
            // we don't know if it's in the internal cache or
            // one of the providers.  If a context is given,
            // then we can narrow down to at most one provider.
            // If the context is null, then we don't know which
            // provider and we have to check all.

            HttpRuntime.CacheInternal.Remove(key);

            if (context == null)
            {
                // remove from all providers since we don't know which one it's in.
                OutputCacheProviderCollection providers = Providers;
                if (providers != null)
                {
                    foreach (OutputCacheProvider provider in providers)
                    {
                        provider.Remove(key);
                    }
                }
            }
            else
            {
                OutputCacheProvider provider = GetProvider(context);
                if (provider != null)
                {
                    provider.Remove(key);
                }
            }
#if DBG
            Debug.Trace("OutputCache", "Remove(" + key + ", context)");
#endif
        }
Exemple #4
0
		OutputCacheProvider FindCacheProvider (HttpApplication app)
		{				
#if NET_4_0
			HttpContext ctx = HttpContext.Current;
			if (app == null) {
				app = ctx != null ? ctx.ApplicationInstance : null;

				if (app == null)
					throw new InvalidOperationException ("Unable to find output cache provider.");
			}

			string providerName = app.GetOutputCacheProviderName (ctx);
			if (String.IsNullOrEmpty (providerName))
				throw new ProviderException ("Invalid OutputCacheProvider name. Name must not be null or an empty string.");

			OutputCacheProvider ret = OutputCache.GetProvider (providerName);
			if (ret == null)
				throw new ProviderException (String.Format ("OutputCacheProvider named '{0}' cannot be found.", providerName));

			return ret;
#else
			if (provider == null)
				provider = new InMemoryOutputCacheProvider ();
			
			return provider;
#endif
		}
        OutputCacheProvider FindCacheProvider(HttpApplication app)
        {
            HttpContext ctx = HttpContext.Current;

            if (app == null)
            {
                app = ctx != null ? ctx.ApplicationInstance : null;

                if (app == null)
                {
                    throw new InvalidOperationException("Unable to find output cache provider.");
                }
            }

            string providerName = app.GetOutputCacheProviderName(ctx);

            if (String.IsNullOrEmpty(providerName))
            {
                throw new ProviderException("Invalid OutputCacheProvider name. Name must not be null or an empty string.");
            }

            OutputCacheProvider ret = OutputCache.GetProvider(providerName);

            if (ret == null)
            {
                throw new ProviderException(String.Format("OutputCacheProvider named '{0}' cannot be found.", providerName));
            }

            return(ret);
        }
Exemple #6
0
        private static OutputCacheProvider GetFragmentProvider(String providerName)
        {
            // if providerName is null, use default provider.  If default provider is null, we'll use internal cache.
            // if providerName is not null, get it from the provider collection.
            OutputCacheProvider provider = null;

            if (providerName == null)
            {
                provider = s_defaultProvider;
            }
            else
            {
                provider = s_providers[providerName];
                if (provider == null)
                {
                    Debug.Assert(false, "Unexpected, " + providerName + " should be a member of the collection.");
                    throw new ProviderException(SR.GetString(SR.Provider_Not_Found, providerName));
                }
            }
#if DBG
            string msg = (provider != null) ? provider.GetType().Name : "null";
            Debug.Trace("OutputCache", "GetFragmentProvider(" + providerName + ") --> " + msg);
#endif
            return(provider);
        }
Exemple #7
0
        internal static object Get(string key)
        {
            object obj2 = null;
            OutputCacheProvider provider = GetProvider(HttpContext.Current);

            if (provider != null)
            {
                obj2 = provider.Get(key);
                OutputCacheEntry oce = obj2 as OutputCacheEntry;
                if (oce != null)
                {
                    if (HasDependencyChanged(false, oce.DependenciesKey, oce.Dependencies, oce.KernelCacheUrl, key, provider.Name))
                    {
                        RemoveFromProvider(key, provider.Name);
                        return(null);
                    }
                    obj2 = Convert(oce);
                }
            }
            if (obj2 == null)
            {
                obj2 = HttpRuntime.CacheInternal.Get(key);
            }
            return(obj2);
        }
Exemple #8
0
        internal static void RemoveFragment(string key, string providerName)
        {
            OutputCacheProvider fragmentProvider = GetFragmentProvider(providerName);

            if (fragmentProvider != null)
            {
                fragmentProvider.Remove(key);
            }
            HttpRuntime.CacheInternal.Remove(key);
        }
Exemple #9
0
        internal static void RemoveFromProvider(string key, string providerName)
        {
            if (providerName == null)
            {
                throw new ArgumentNullException("providerName");
            }
            OutputCacheProviderCollection providers = Providers;
            OutputCacheProvider           provider  = (providers == null) ? null : providers[providerName];

            if (provider == null)
            {
                throw new ProviderException(System.Web.SR.GetString("Provider_Not_Found", new object[] { providerName }));
            }
            provider.Remove(key);
        }
Exemple #10
0
        private static OutputCacheProvider GetFragmentProvider(string providerName)
        {
            OutputCacheProvider provider = null;

            if (providerName == null)
            {
                return(s_defaultProvider);
            }
            provider = s_providers[providerName];
            if (provider == null)
            {
                throw new ProviderException(System.Web.SR.GetString("Provider_Not_Found", new object[] { providerName }));
            }
            return(provider);
        }
Exemple #11
0
        // remove fragment
        internal static void RemoveFragment(String key, String providerName)
        {
            // if providerName is null, use default provider.
            // if providerName is not null, get it from the provider collection.
            // remove it from the provider and the internal cache (we don't know where it is).
            OutputCacheProvider provider = GetFragmentProvider(providerName);

            if (provider != null)
            {
                provider.Remove(key);
            }
            HttpRuntime.CacheInternal.Remove(key);
#if DBG
            Debug.Trace("OutputCache", "RemoveFragment(" + key + "," + providerName + ")");
#endif
        }
Exemple #12
0
 private static void EnsureInitialized()
 {
     if (!s_inited)
     {
         lock (s_initLock)
         {
             if (!s_inited)
             {
                 OutputCacheSection outputCache = RuntimeConfig.GetAppConfig().OutputCache;
                 s_providers                            = outputCache.CreateProviderCollection();
                 s_defaultProvider                      = outputCache.GetDefaultProvider(s_providers);
                 s_entryRemovedCallback                 = new CacheItemRemovedCallback(OutputCache.EntryRemovedCallback);
                 s_dependencyRemovedCallback            = new CacheItemRemovedCallback(OutputCache.DependencyRemovedCallback);
                 s_dependencyRemovedCallbackForFragment = new CacheItemRemovedCallback(OutputCache.DependencyRemovedCallbackForFragment);
                 s_inited = true;
             }
         }
     }
 }
        static OutputCache()
        {
            var providerType = new CacheSettingsManager().RetrieveOutputCacheProviderType();

            if (providerType == null)
            {
                instance = new MemoryCacheProvider();
            }
            else
            {
                try
                {
                    instance = (OutputCacheProvider)Activator.CreateInstance(Type.GetType(providerType));
                }
                catch (Exception ex)
                {
                    throw new ConfigurationErrorsException(string.Format("Unable to instantiate OutputCacheProvider of type '{0}'. Make sure you are specifying the full type name.", providerType), ex);
                }

            }
        }
Exemple #14
0
        internal static object GetFragment(string key, string providerName)
        {
            object obj2 = null;
            OutputCacheProvider fragmentProvider = GetFragmentProvider(providerName);

            if (fragmentProvider != null)
            {
                obj2 = fragmentProvider.Get(key);
                PartialCachingCacheEntry entry = obj2 as PartialCachingCacheEntry;
                if ((entry != null) && HasDependencyChanged(true, entry._dependenciesKey, entry._dependencies, null, key, fragmentProvider.Name))
                {
                    RemoveFragment(key, fragmentProvider.Name);
                    return(null);
                }
            }
            if (obj2 == null)
            {
                obj2 = HttpRuntime.CacheInternal.Get(key);
            }
            return(obj2);
        }
Exemple #15
0
        // lookup fragment
        internal static Object GetFragment(String key, String providerName)
        {
            // if providerName is null, use default provider.
            // if providerName is not null, get it from the provider collection.
            // if it's not in the provider or the default provider is undefined,
            // check the internal cache (we don't know where it is).
            Object result = null;
            OutputCacheProvider provider = GetFragmentProvider(providerName);

            if (provider != null)
            {
                result = provider.Get(key);
                PartialCachingCacheEntry fragment = result as PartialCachingCacheEntry;
                if (fragment != null)
                {
                    if (HasDependencyChanged(true /*isFragment*/, fragment._dependenciesKey, fragment._dependencies, null /*kernelKey*/, key, provider.Name))
                    {
                        OutputCache.RemoveFragment(key, provider.Name);
#if DBG
                        Debug.Trace("OutputCache", "GetFragment(" + key + "," + providerName + ") --> null, " + providerName);
#endif
                        return(null);
                    }
                }
            }

            if (result == null)
            {
                result = HttpRuntime.CacheInternal.Get(key);
#if DBG
                string typeName = (result != null) ? result.GetType().Name : "null";
                Debug.Trace("OutputCache", "GetFragment(" + key + "," + providerName + ") --> " + typeName + ", CacheInternal");
            }
            else
            {
                Debug.Trace("OutputCache", "GetFragment(" + key + "," + providerName + ") --> " + result.GetType().Name + ", " + providerName);
#endif
            }
            return(result);
        }
Exemple #16
0
        void OnBuildManagerRemoveEntry(BuildManagerRemoveEntryEventArgs args)
        {
            string      entry   = args.EntryName;
            HttpContext context = args.Context;
            string      cacheValue;

            lock (keysCacheLock)
            {
                if (!keysCache.TryGetValue(entry, out cacheValue))
                {
                    return;
                }

                keysCache.Remove(entry);
                if (context == null)
                {
                    if (entriesToInvalidate == null)
                    {
                        entriesToInvalidate = new Dictionary <string, string> (StringComparer.Ordinal);
                        entriesToInvalidate.Add(entry, cacheValue);
                        return;
                    }
                    else if (!entriesToInvalidate.ContainsKey(entry))
                    {
                        entriesToInvalidate.Add(entry, cacheValue);
                        return;
                    }
                }
            }

            OutputCacheProvider provider = FindCacheProvider(context != null ? context.ApplicationInstance : null);

            provider.Remove(entry);
            if (!String.IsNullOrEmpty(cacheValue))
            {
                provider.Remove(cacheValue);
            }
        }
Exemple #17
0
        // lookup cached vary
        // lookup entry
        // lookup entry for content-encoding
        internal static Object Get(String key)
        {
            // if it's not in the provider or the default provider is undefined,
            // check the internal cache (we don't know where it is).
            Object result = null;
            OutputCacheProvider provider = GetProvider(HttpContext.Current);

            if (provider != null)
            {
                result = provider.Get(key);
                OutputCacheEntry oce = result as OutputCacheEntry;
                if (oce != null)
                {
                    if (HasDependencyChanged(false /*isFragment*/, oce.DependenciesKey, oce.Dependencies, oce.KernelCacheUrl, key, provider.Name))
                    {
                        OutputCache.RemoveFromProvider(key, provider.Name);
#if DBG
                        Debug.Trace("OutputCache", "Get(" + key + ") --> null, " + provider.Name);
#endif
                        return(null);
                    }
                    result = Convert(oce);
                }
            }
            if (result == null)
            {
                result = HttpRuntime.CacheInternal.Get(key);
#if DBG
                string typeName = (result != null) ? result.GetType().Name : "null";
                Debug.Trace("OutputCache", "Get(" + key + ") --> " + typeName + ", CacheInternal");
            }
            else
            {
                Debug.Trace("OutputCache", "Get(" + key + ") --> " + result.GetType().Name + ", " + provider.Name);
#endif
            }
            return(result);
        }
Exemple #18
0
        void OnRawResponseRemoved(string key, object value, CacheItemRemovedReason reason)
        {
            CachedRawResponse c      = value as CachedRawResponse;
            CachedVaryBy      varyby = c != null ? c.VaryBy : null;

            if (varyby == null)
            {
                return;
            }

            List <string>       itemList = varyby.ItemList;
            OutputCacheProvider provider = FindCacheProvider(null);

            itemList.Remove(key);
            provider.Remove(key);

            if (itemList.Count != 0)
            {
                return;
            }

            provider.Remove(varyby.Key);
        }
Exemple #19
0
 internal static void Remove(string key, HttpContext context)
 {
     HttpRuntime.CacheInternal.Remove(key);
     if (context == null)
     {
         OutputCacheProviderCollection providers = Providers;
         if (providers != null)
         {
             foreach (OutputCacheProvider provider in providers)
             {
                 provider.Remove(key);
             }
         }
     }
     else
     {
         OutputCacheProvider provider2 = GetProvider(context);
         if (provider2 != null)
         {
             provider2.Remove(key);
         }
     }
 }
Exemple #20
0
        // remove cache vary
        // remove entry
        internal static void RemoveFromProvider(String key, String providerName)
        {
            // we know where it is.  If providerName is given,
            // then it is in that provider.  If it's not given,
            // it's in the internal cache.
            if (providerName == null)
            {
                throw new ArgumentNullException("providerName");
            }

            OutputCacheProviderCollection providers = Providers;
            OutputCacheProvider           provider  = (providers == null) ? null : providers[providerName];

            if (provider == null)
            {
                throw new ProviderException(SR.GetString(SR.Provider_Not_Found, providerName));
            }

            provider.Remove(key);
#if DBG
            Debug.Trace("OutputCache", "Remove(" + key + ", " + providerName + ")");
#endif
        }
Exemple #21
0
        private static OutputCacheProvider GetProvider(HttpContext context)
        {
            if (context == null)
            {
                return(null);
            }
            string outputCacheProviderName = context.ApplicationInstance.GetOutputCacheProviderName(context);

            switch (outputCacheProviderName)
            {
            case null:
                throw new ProviderException(System.Web.SR.GetString("GetOutputCacheProviderName_Invalid", new object[] { outputCacheProviderName }));

            case "AspNetInternalProvider":
                return(null);
            }
            OutputCacheProvider provider = (s_providers == null) ? null : s_providers[outputCacheProviderName];

            if (provider == null)
            {
                throw new ProviderException(System.Web.SR.GetString("GetOutputCacheProviderName_Invalid", new object[] { outputCacheProviderName }));
            }
            return(provider);
        }
        private static void EnsureInitialized() {
            if (s_inited) 
                return;

            lock (s_initLock) {
                if (!s_inited) {
                    OutputCacheSection settings = RuntimeConfig.GetAppConfig().OutputCache;
                    s_providers = settings.CreateProviderCollection();
                    s_defaultProvider = settings.GetDefaultProvider(s_providers);
                    s_entryRemovedCallback = new CacheItemRemovedCallback(OutputCache.EntryRemovedCallback);
                    s_dependencyRemovedCallback = new CacheItemRemovedCallback(OutputCache.DependencyRemovedCallback);
                    s_dependencyRemovedCallbackForFragment = new CacheItemRemovedCallback(OutputCache.DependencyRemovedCallbackForFragment);
                    s_inited = true;
                }
            }
        }
 public OutputCacheManager()
 {
     _outputCacheProvider = OutputCache.Instance;
     _keyBuilder = new KeyBuilder();
 }
 public OutputCacheManager(OutputCacheProvider outputCacheProvider, IKeyBuilder keyBuilder)
 {
     _outputCacheProvider = outputCacheProvider;
     _keyBuilder = keyBuilder;
 }
Exemple #25
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
        }
Exemple #26
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
        }
Exemple #27
0
        void OnResolveRequestCache(object o, EventArgs args)
        {
            HttpApplication app     = o as HttpApplication;
            HttpContext     context = app != null ? app.Context : null;

            if (context == null)
            {
                return;
            }

            OutputCacheProvider provider = FindCacheProvider(app);
            string            vary_key   = context.Request.FilePath;
            CachedVaryBy      varyby     = provider.Get(vary_key) as CachedVaryBy;
            string            key;
            CachedRawResponse c;

            if (varyby == null)
            {
                return;
            }

            key = varyby.CreateKey(vary_key, context);
            c   = provider.Get(key) as CachedRawResponse;
            if (c == null)
            {
                return;
            }

            lock (keysCacheLock) {
                string invValue;
                if (entriesToInvalidate != null && entriesToInvalidate.TryGetValue(vary_key, out invValue) && String.Compare(invValue, key, StringComparison.Ordinal) == 0)
                {
                    provider.Remove(vary_key);
                    provider.Remove(key);
                    entriesToInvalidate.Remove(vary_key);
                    return;
                }
            }

            ArrayList callbacks = c.Policy.ValidationCallbacks;

            if (callbacks != null && callbacks.Count > 0)
            {
                bool isValid   = true;
                bool isIgnored = false;

                foreach (Pair p in callbacks)
                {
                    HttpCacheValidateHandler validate = (HttpCacheValidateHandler)p.First;
                    object data = p.Second;
                    HttpValidationStatus status = HttpValidationStatus.Valid;

                    try {
                        validate(context, data, ref status);
                    } catch {
                        // MS.NET hides the exception
                        isValid = false;
                        break;
                    }

                    if (status == HttpValidationStatus.Invalid)
                    {
                        isValid = false;
                        break;
                    }
                    else if (status == HttpValidationStatus.IgnoreThisRequest)
                    {
                        isIgnored = true;
                    }
                }

                if (!isValid)
                {
                    OnRawResponseRemoved(key, c, CacheItemRemovedReason.Removed);
                    return;
                }
                else if (isIgnored)
                {
                    return;
                }
            }

            HttpResponse response = context.Response;

            response.ClearContent();
            IList cachedData = c.GetData();

            if (cachedData != null)
            {
                Encoding outEnc = WebEncoding.ResponseEncoding;

                foreach (CachedRawResponse.DataItem d in cachedData)
                {
                    if (d.Length > 0)
                    {
                        response.BinaryWrite(d.Buffer, 0, (int)d.Length);
                        continue;
                    }

                    if (d.Callback == null)
                    {
                        continue;
                    }

                    string s = d.Callback(context);
                    if (s == null || s.Length == 0)
                    {
                        continue;
                    }

                    byte[] bytes = outEnc.GetBytes(s);
                    response.BinaryWrite(bytes, 0, bytes.Length);
                }
            }

            response.ClearHeaders();
            response.SetCachedHeaders(c.Headers);
            response.StatusCode        = c.StatusCode;
            response.StatusDescription = c.StatusDescription;

            app.CompleteRequest();
        }
Exemple #28
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);
                    }
                }
            }
        }
Exemple #29
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();
                }
            }
        }
Exemple #30
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();
                }
            }
        }
		public void CopyTo (OutputCacheProvider[] array, int index)
		{
			base.CopyTo (array, index);
		}