public CacheSettingsManager()
 {
     try
     {
         _outputCacheSection = (OutputCacheSection)ConfigurationManager.GetSection("system.web/caching/outputCache");
     }
     catch (SecurityException)
     {
         Trace.WriteLine("MvcDonutCaching does not have permission to read web.config section 'OutputCacheSection'. Using default provider.");
         _outputCacheSection = new OutputCacheSection {
             DefaultProviderName = AspnetInternalProviderName, EnableOutputCache = true
         };
     }
 }
Example #2
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;
             }
         }
     }
 }
Example #3
0
    public ChildActionOutputCacheAttribute(string cacheProfile)
    {
        // get output cache section of web config
        OutputCacheSection settings = (OutputCacheSection)WebConfigurationManager.GetSection($"{_cachingSection}{_outputCacheSection}");

        // check section exists and caching is enabled
        if (settings != null && settings.EnableOutputCache)
        {
            // if caching enabled, get profile
            OutputCacheSettingsSection profileSettings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection($"{_cachingSection}{_profileSection}");
            OutputCacheProfile         profile         = profileSettings.OutputCacheProfiles[cacheProfile];
            if (profile != null && profile.Enabled)
            {
                // if profile exits set profile params
                Duration        = profile.Duration;
                VaryByParam     = profile.VaryByParam;
                VaryByCustom    = profile.VaryByCustom;
                _profileEnabled = true;               // set profile enable to true as output cache is turned on and there is a profile
            }
        }
    }
        static void Main(string[] args)
        {
            string inputStr = String.Empty;

            // Define a regular expression to allow only
            // alphanumeric inputs that are at most 20 character
            // long. For instance "/iii:".
            Regex rex = new Regex(@"[^\/w]{1,20}");

            // Parse the user's input.
            if (args.Length < 1)
            {
                // No option entered.
                Console.Write("Input parameters missing.");
                return;
            }
            else
            {
                // Get the user's options.
                inputStr = args[0].ToLower();

                if (!(rex.Match(inputStr)).Success)
                {
                    // Wrong option format used.
                    Console.Write("Input format not allowed.");
                    return;
                }
            }

            // <Snippet1>

            // Get the Web application configuration.
            System.Configuration.Configuration configuration =
                WebConfigurationManager.OpenWebConfiguration(
                    "/aspnetTest");

            // Get the <caching> section group.
            SystemWebCachingSectionGroup cachingSectionGroup =
                (SystemWebCachingSectionGroup)configuration.GetSectionGroup(
                    "system.web/caching");

            // </Snippet1>

            try
            {
                switch (inputStr)
                {
                case "/cache":

                    // <Snippet2>

                    // Get the <cache> section.
                    CacheSection cache =
                        cachingSectionGroup.Cache;

                    // Display one of its properties.
                    msg = String.Format(
                        "Cache disable expiration: {0}\n",
                        cache.DisableExpiration);

                    Console.Write(msg);

                    // </Snippet2>

                    break;

                case "/outcache":

                    // <Snippet3>

                    // Get the .<outputCache> section
                    OutputCacheSection outputCache =
                        cachingSectionGroup.OutputCache;

                    // Display one of its properties.
                    msg = String.Format(
                        "Enable output cache: {0}\n",
                        outputCache.EnableOutputCache.ToString());

                    Console.Write(msg);

                    // </Snippet3>

                    break;

                case "/outcacheset":

                    // <Snippet4>

                    // Get the .<outputCacheSettings> section
                    OutputCacheSettingsSection outputCacheSettings =
                        cachingSectionGroup.OutputCacheSettings;

                    // Display the number of existing
                    // profiles.
                    int profilesCount =
                        outputCacheSettings.OutputCacheProfiles.Count;
                    msg = String.Format(
                        "Number of profiles: {0}\n",
                        profilesCount.ToString());

                    Console.Write(msg);

                    // </Snippet4>

                    break;

                case "/sql":

                    // <Snippet5>

                    // Get the .<sqlCacheDependency> section
                    SqlCacheDependencySection sqlCacheDependency =
                        cachingSectionGroup.SqlCacheDependency;

                    // Display one of its attributes.
                    msg = String.Format(
                        "Sql cache dependency enabled: {0}\n",
                        sqlCacheDependency.Enabled.ToString());

                    Console.Write(msg);

                    // </Snippet5>

                    break;

                //  case "/all":

                // <Snippet6>

                // Not in use anymore.
                // StringBuilder allSections = new StringBuilder();

                // Get the section collection.
                //  ConfigurationSectionCollection sections=
                //    cachingSectionGroup.Sections;

                // Get the number of sections.
                // int sectionsNumber = sections.Count;

                //  System.Collections.IEnumerator ienum =
                //     sections.AllKeys.GetEnumerator();

                // int i = 0;
                // allSections.AppendLine();
                // foreach (Object section in sections)
                // {
                //    msg = String.Format(
                //     "Section{0}:  {1}\n",
                //    i, section.ToString());
                //    allSections.AppendLine(msg);
                //    i++;
                // }

                // </Snippet6>

                // Console.Write(allSections.ToString());
                //     break;

                default:
                    // Option is not allowed..
                    Console.Write("Input not allowed.");
                    break;
                }
            }
            catch (ArgumentException e)
            {
                // Never display this. Use it for
                // debugging purposes.
                msg = e.ToString();
            }
        }
Example #5
0
        private void UpdateCachedHeaders(HttpResponse response)
        {
            if (!this._useCachedHeaders)
            {
                HttpCacheability @private;
                int num;
                int size;
                if (this._utcTimestampCreated == DateTime.MinValue)
                {
                    this._utcTimestampCreated = this._utcTimestampRequest = response.Context.UtcTimestamp;
                }
                if (this._slidingExpiration != 1)
                {
                    this._slidingDelta = TimeSpan.Zero;
                }
                else if (this._isMaxAgeSet)
                {
                    this._slidingDelta = this._maxAge;
                }
                else if (this._isExpiresSet)
                {
                    this._slidingDelta = (TimeSpan)(this._utcExpires - this._utcTimestampCreated);
                }
                else
                {
                    this._slidingDelta = TimeSpan.Zero;
                }
                this._headerCacheControl = null;
                this._headerPragma       = null;
                this._headerExpires      = null;
                this._headerLastModified = null;
                this._headerEtag         = null;
                this._headerVaryBy       = null;
                this.UpdateFromDependencies(response);
                StringBuilder s = new StringBuilder();
                if (this._cacheability == (HttpCacheability.Public | HttpCacheability.Private))
                {
                    @private = HttpCacheability.Private;
                }
                else
                {
                    @private = this._cacheability;
                }
                AppendValueToHeader(s, s_cacheabilityTokens[(int)@private]);
                if ((@private == HttpCacheability.Public) && (this._privateFields != null))
                {
                    AppendValueToHeader(s, "private=\"");
                    s.Append(this._privateFields.GetKey(0));
                    num  = 1;
                    size = this._privateFields.Size;
                    while (num < size)
                    {
                        AppendValueToHeader(s, this._privateFields.GetKey(num));
                        num++;
                    }
                    s.Append('"');
                }
                if (((@private != HttpCacheability.NoCache) && (@private != HttpCacheability.Server)) && (this._noCacheFields != null))
                {
                    AppendValueToHeader(s, "no-cache=\"");
                    s.Append(this._noCacheFields.GetKey(0));
                    num  = 1;
                    size = this._noCacheFields.Size;
                    while (num < size)
                    {
                        AppendValueToHeader(s, this._noCacheFields.GetKey(num));
                        num++;
                    }
                    s.Append('"');
                }
                if (this._noStore)
                {
                    AppendValueToHeader(s, "no-store");
                }
                AppendValueToHeader(s, s_revalidationTokens[(int)this._revalidation]);
                if (this._noTransforms)
                {
                    AppendValueToHeader(s, "no-transform");
                }
                if (this._cacheExtension != null)
                {
                    AppendValueToHeader(s, this._cacheExtension);
                }
                if (((this._slidingExpiration == 1) && (@private != HttpCacheability.NoCache)) && (@private != HttpCacheability.Server))
                {
                    if (this._isMaxAgeSet && !this._noMaxAgeInCacheControl)
                    {
                        AppendValueToHeader(s, "max-age=" + ((long)this._maxAge.TotalSeconds).ToString(CultureInfo.InvariantCulture));
                    }
                    if (this._isProxyMaxAgeSet && !this._noMaxAgeInCacheControl)
                    {
                        AppendValueToHeader(s, "s-maxage=" + ((long)this._proxyMaxAge.TotalSeconds).ToString(CultureInfo.InvariantCulture));
                    }
                }
                if (s.Length > 0)
                {
                    this._headerCacheControl = new HttpResponseHeader(0, s.ToString());
                }
                switch (@private)
                {
                case HttpCacheability.NoCache:
                case HttpCacheability.Server:
                    if (s_headerPragmaNoCache == null)
                    {
                        s_headerPragmaNoCache = new HttpResponseHeader(4, "no-cache");
                    }
                    this._headerPragma = s_headerPragmaNoCache;
                    if (this._allowInHistory != 1)
                    {
                        if (s_headerExpiresMinus1 == null)
                        {
                            s_headerExpiresMinus1 = new HttpResponseHeader(0x12, "-1");
                        }
                        this._headerExpires = s_headerExpiresMinus1;
                    }
                    break;

                default:
                    if (this._isExpiresSet && (this._slidingExpiration != 1))
                    {
                        string str = HttpUtility.FormatHttpDateTimeUtc(this._utcExpires);
                        this._headerExpires = new HttpResponseHeader(0x12, str);
                    }
                    if (this._isLastModifiedSet)
                    {
                        string str2 = HttpUtility.FormatHttpDateTimeUtc(this._utcLastModified);
                        this._headerLastModified = new HttpResponseHeader(0x13, str2);
                    }
                    if (@private != HttpCacheability.Private)
                    {
                        bool omitVaryStar;
                        if (this._etag != null)
                        {
                            this._headerEtag = new HttpResponseHeader(0x16, this._etag);
                        }
                        string str3 = null;
                        if (this._omitVaryStar != -1)
                        {
                            omitVaryStar = this._omitVaryStar == 1;
                        }
                        else
                        {
                            OutputCacheSection outputCache = RuntimeConfig.GetLKGConfig(response.Context).OutputCache;
                            if (outputCache != null)
                            {
                                omitVaryStar = outputCache.OmitVaryStar;
                            }
                            else
                            {
                                omitVaryStar = false;
                            }
                        }
                        if (!omitVaryStar && ((this._varyByCustom != null) || (this._varyByParams.IsModified() && !this._varyByParams.IgnoreParams)))
                        {
                            str3 = "*";
                        }
                        if (str3 == null)
                        {
                            str3 = this._varyByHeaders.ToHeaderString();
                        }
                        if (str3 != null)
                        {
                            this._headerVaryBy = new HttpResponseHeader(0x1c, str3);
                        }
                    }
                    break;
                }
                this._useCachedHeaders = true;
            }
        }