コード例 #1
0
        /// <summary>
        /// Sets the output cache parameters and also the client side caching parameters
        /// </summary>
        /// <param name="context"></param>
        private void SetCaching(HttpContext context, string fileName)
        {
            //This ensures OutputCaching is set for this handler and also controls
            //client side caching on the browser side. Default is 10 days.
            TimeSpan        duration = TimeSpan.FromDays(10);
            HttpCachePolicy cache    = context.Response.Cache;

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(duration));
            cache.SetMaxAge(duration);
            cache.SetValidUntilExpires(true);
            cache.SetLastModified(DateTime.Now);
            cache.SetETag(Guid.NewGuid().ToString());
            //set server OutputCache to vary by our params
            cache.VaryByParams["t"] = true;
            cache.VaryByParams["s"] = true;
            //don't allow varying by wildcard
            cache.SetOmitVaryStar(true);
            //ensure client browser maintains strict caching rules
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            //This is the only way to set the max-age cachability header in ASP.Net!
            FieldInfo maxAgeField = cache.GetType().GetField("_maxAge", BindingFlags.Instance | BindingFlags.NonPublic);

            maxAgeField.SetValue(cache, duration);

            //make this output cache dependent on the file if there is one.
            if (!string.IsNullOrEmpty(fileName))
            {
                context.Response.AddFileDependency(fileName);
            }
        }
コード例 #2
0
        /// <summary>
        /// This will make the browser and server keep the output
        /// in its cache and thereby improve performance.
        /// </summary>
        private void SetHeadersAndCache(string file, HttpContext context)
        {
            //string ext = file.Substring(file.LastIndexOf(".")).ToString().ToLower();

            //TimeSpan maxAge = new TimeSpan(3, 0, 0, 0);

            //context.Response.Cache.SetMaxAge(maxAge);

            //context.Response.AddFileDependency(file);
            //context.Response.Cache.SetCacheability(HttpCacheability.Public);

            //context.Response.Cache.VaryByParams["path"] = true;
            //context.Response.Cache.SetETagFromFileDependencies();
            //context.Response.Cache.SetExpires(DateTime.Now.AddDays(3));
            //context.Response.Expires = 86400000;
            //context.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
            //context.Response.Cache.SetETag(string.Empty);
            //context.Response.Cache.SetLastModifiedFromFileDependencies();

            HttpResponse response = context.Response;

            TimeSpan duration = TimeSpan.FromDays(3);

            HttpCachePolicy cache = response.Cache;

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(duration));
            cache.SetMaxAge(duration);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");

            FieldInfo maxAgeField = cache.GetType().GetField("_maxAge", BindingFlags.Instance | BindingFlags.NonPublic);

            maxAgeField.SetValue(cache, duration);
        }
コード例 #3
0
        public static DateTime GetLastModified(this HttpCachePolicy self)
        {
            var type  = self.GetType();
            var flags = BindingFlags.Instance | BindingFlags.NonPublic;

            var cacheability = type.GetField("_utcLastModified", flags);

            return((DateTime)cacheability.GetValue(self));
        }
コード例 #4
0
        public static HttpCacheability GetCacheability(this HttpCachePolicy self)
        {
            var type  = self.GetType();
            var flags = BindingFlags.Instance | BindingFlags.NonPublic;

            var cacheability = type.GetField("_cacheability", flags);

            return((HttpCacheability)cacheability.GetValue(self));
        }
コード例 #5
0
ファイル: CacheUtility.cs プロジェクト: nhatgiaoict/gencenter
    public static void Cache(TimeSpan duration)
    {
        HttpCachePolicy cache = HttpContext.Current.Response.Cache;

        FieldInfo maxAgeField = cache.GetType().GetField("_maxAge", BindingFlags.Instance | BindingFlags.NonPublic);

        maxAgeField.SetValue(cache, duration);

        cache.SetCacheability(HttpCacheability.Public);
        cache.SetExpires(DateTime.Now.Add(duration));
        cache.SetMaxAge(duration);
        cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
    }
コード例 #6
0
        internal HttpCacheability GetCacheability(HttpCachePolicy cache)
        {
            FieldInfo cacheabilityInfo = cache.GetType().GetField("_cacheability",
                                                                  BindingFlags.NonPublic |
                                                                  BindingFlags.Instance);

            if (cacheabilityInfo != null)
            {
                return((HttpCacheability)Enum.Parse(typeof(HttpCacheability), cacheabilityInfo.GetValue(cache).ToString()));
            }

            return(HttpCacheability.NoCache);
        }
コード例 #7
0
        internal bool HasValidationPolicy(HttpCachePolicy cache)
        {
            MethodInfo hasValidationPolicyInfo = cache.GetType().GetMethod("HasValidationPolicy",
                                                                           BindingFlags.NonPublic |
                                                                           BindingFlags.Instance |
                                                                           BindingFlags.InvokeMethod);

            if (hasValidationPolicyInfo != null)
            {
                return((bool)hasValidationPolicyInfo.Invoke(cache, null));
            }

            return(false);
        }
コード例 #8
0
        internal bool GetNoServerCaching(HttpCachePolicy cache)
        {
            MethodInfo getNoServerCachingInfo = cache.GetType().GetMethod("GetNoServerCaching",
                                                                          BindingFlags.NonPublic |
                                                                          BindingFlags.Instance |
                                                                          BindingFlags.InvokeMethod);

            if (getNoServerCachingInfo != null)
            {
                return((bool)getNoServerCachingInfo.Invoke(cache, null));
            }

            return(true);
        }
コード例 #9
0
        internal bool IsModified(HttpCachePolicy cache)
        {
            MethodInfo isModifiedInfo = cache.GetType().GetMethod("IsModified",
                                                                  BindingFlags.NonPublic |
                                                                  BindingFlags.Instance |
                                                                  BindingFlags.InvokeMethod);

            if (isModifiedInfo != null)
            {
                return((bool)isModifiedInfo.Invoke(cache, null));
            }

            return(false);
        }