Example #1
0
        /// <summary>
        /// Ensures that the cache is not expired.
        /// </summary>
        private void EnsureExpiration()
        {
            bool cacheExpired = false;
            long maxAge       = -1,
                 expires      = -1;
            string key        = "HttpCache:EnsureExpiration";

            if (!TemporaryStorage.Contains(key))
            {
                cacheExpired = !CanCache();

                if (!cacheExpired)
                {
                    if (Flags.ContainsKey("max-age") && long.TryParse(Flags["max-age"], out maxAge) && maxAge > 0)
                    {
                        cacheExpired = new DateTime(Created + maxAge).Ticks < DateTime.UtcNow.Ticks;
                    }
                    else if (Flags.ContainsKey("expires") && long.TryParse(Flags["expires"], out expires))
                    {
                        cacheExpired = new DateTime(expires).Ticks < DateTime.UtcNow.Ticks;
                    }
                }

                if (cacheExpired)
                {
                    ClearItems();
                }

                TemporaryStorage.Set(key, true);
            }
        }
Example #2
0
 /// <summary>
 /// Adds or updates the item with the given key.
 /// </summary>
 /// <param name="key">Item key.</param>
 /// <param name="value">Item value.</param>
 protected override void SetItem(string key, object value)
 {
     if (CanCache())
     {
         base.SetItem(key, value);
     }
     else
     {
         TemporaryStorage.Set(key, value);
     }
 }
Example #3
0
        /// <summary>
        /// Returns value indicating whether items can be added or updated.
        /// </summary>
        /// <returns>Value indicating whether items can be added or updated.</returns>
        private bool CanCache()
        {
            bool   ret = true;
            string key = "HttpCache:CanCache";

            if (!TemporaryStorage.Contains(key))
            {
                TemporaryStorage.Set(key, !Flags.ContainsKey("no-cache") && !Flags.ContainsKey("no-store"));
            }

            ret = TemporaryStorage.Get <bool>(key);

            return(ret);
        }
Example #4
0
        /// <summary>
        /// Ensures that flags are loaded.
        /// </summary>
        private void EnsureFlags()
        {
            string[] v            = null;
            string   expires      = string.Empty;
            string   cacheControl = string.Empty;
            string   key          = "HttpCache:EnsureFlags";
            DateTime expiresDate  = DateTime.MinValue;

            if (!TemporaryStorage.Contains(key))
            {
                expires      = HttpContext.Current.Request.Headers["Expires"];
                cacheControl = HttpContext.Current.Request.Headers["Cache-Control"];

                Flags = new Dictionary <string, string>();

                if (!string.IsNullOrEmpty(cacheControl))
                {
                    foreach (string pair in cacheControl.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        v = pair.ToLowerInvariant().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                        if (v.Any())
                        {
                            if (!Flags.ContainsKey(v[0]))
                            {
                                Flags.Add(v[0], v.Length > 1 ? v[1] : string.Empty);
                            }
                            else
                            {
                                Flags[v[0]] = v[1];
                            }
                        }
                    }
                }

                if (!Flags.ContainsKey("expires") && !string.IsNullOrEmpty(expires) && DateTime.TryParseExact(expires, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture, DateTimeStyles.None, out expiresDate))
                {
                    Flags.Add("expires", expiresDate.Ticks.ToString());
                }

                TemporaryStorage.Set(key, true);
            }
        }