protected void SetClientCacheHeader(DateTime?LastModified, string Etag, HttpCacheability CacheKind, bool ReValidate = true) { if (!EnableClientCache || LastModified == null && Etag == null) { return; } HttpCachePolicyBase cp = Response.Cache; cp.AppendCacheExtension("max-age=" + 3600 * MaxClientCacheAgeInHours); if (ReValidate) { cp.AppendCacheExtension("must-revalidate"); cp.AppendCacheExtension("proxy-revalidate"); } cp.SetCacheability(CacheKind); cp.SetOmitVaryStar(false); if (LastModified != null) { cp.SetLastModified(LastModified.Value); } cp.SetExpires(DateTime.UtcNow.AddHours(MaxClientCacheAgeInHours)); if (Etag != null) { cp.SetETag(Etag); } }
protected virtual void SetCache(HttpResponseBase response, int cacheDuration, params string[] varyByParams) { // Cache if (cacheDuration > 0) { DateTime timestamp = DateTime.Now; HttpCachePolicyBase cache = response.Cache; int duration = cacheDuration; cache.SetCacheability(HttpCacheability.Public); cache.SetAllowResponseInBrowserHistory(true); cache.SetExpires(timestamp.AddSeconds(duration)); cache.SetMaxAge(new TimeSpan(0, 0, duration)); cache.SetValidUntilExpires(true); cache.SetLastModified(timestamp); cache.VaryByHeaders["Accept-Encoding"] = true; if (varyByParams != null) { foreach (var p in varyByParams) { cache.VaryByParams[p] = true; } } cache.SetOmitVaryStar(true); response.AddHeader("Cache-Control", string.Format("public, max-age={0}", cacheDuration)); } }
public void Apply(HttpCachePolicyBase policy) { policy.ThrowIfNull("policy"); if (!_hasPolicy) { return; } switch (_cacheability) { case HttpCacheability.NoCache: policy.SetCacheability(_allowsServerCaching == true ? HttpCacheability.ServerAndNoCache : HttpCacheability.NoCache); break; case HttpCacheability.Private: policy.SetCacheability(_allowsServerCaching == true ? HttpCacheability.ServerAndPrivate : HttpCacheability.Private); break; case HttpCacheability.Public: policy.SetCacheability(HttpCacheability.Public); break; } if (_noStore == true) { policy.SetNoStore(); } if (_noTransforms == true) { policy.SetNoTransforms(); } if (_clientCacheExpirationUtcTimestamp != null) { policy.SetExpires(_clientCacheExpirationUtcTimestamp.Value); } if (_clientCacheMaxAge != null) { policy.SetMaxAge(_clientCacheMaxAge.Value); } if (_allowResponseInBrowserHistory != null) { policy.SetAllowResponseInBrowserHistory(_allowResponseInBrowserHistory.Value); } if (_eTag != null) { policy.SetETag(_eTag); } if (_omitVaryStar != null) { policy.SetOmitVaryStar(_omitVaryStar.Value); } if (_proxyMaxAge != null) { policy.SetProxyMaxAge(_proxyMaxAge.Value); } if (_revalidation != null) { policy.SetRevalidation(_revalidation.Value); } }
private static void SetHeaders(BundleResponse response, BundleContext context, bool noCache) { if (context.HttpContext.Response != null) { if (response.ContentType != null) { context.HttpContext.Response.ContentType = response.ContentType; } // Do set caching headers if instrumentation mode is on if (!context.EnableInstrumentation && context.HttpContext.Response.Cache != null) { // NOTE: These caching headers were based off of what AssemblyResourceLoader does // TODO: let the bundle change the caching semantics? HttpCachePolicyBase cachePolicy = context.HttpContext.Response.Cache; if (noCache) { cachePolicy.SetCacheability(HttpCacheability.NoCache); } else { cachePolicy.SetCacheability(response.Cacheability); cachePolicy.SetOmitVaryStar(true); cachePolicy.SetExpires(DateTime.Now.AddYears(1)); cachePolicy.SetValidUntilExpires(true); cachePolicy.SetLastModified(DateTime.Now); cachePolicy.VaryByHeaders["User-Agent"] = true; // CONSIDER: Last modified logic, need to compute a hash of all the dates/ETag support } } } }
private void SetBundleHeaders(BundleResponse bundleResponse, BundleContext context) { if (context.HttpContext.Response == null) { return; } if (bundleResponse.ContentType != null) { context.HttpContext.Response.ContentType = bundleResponse.ContentType; } if (context.EnableInstrumentation || context.HttpContext.Response.Cache == null) { return; } HttpCachePolicyBase cache = context.HttpContext.Response.Cache; cache.SetCacheability(bundleResponse.Cacheability); cache.SetOmitVaryStar(true); cache.SetExpires(DateTime.Now.AddYears(1)); cache.SetValidUntilExpires(true); cache.SetLastModified(DateTime.Now); cache.VaryByHeaders["User-Agent"] = true; }
private static void PrepareResponseCache(HttpResponseBase response) { HttpCachePolicyBase cachePolicy = response.Cache; DateTime now = DateTime.Now; cachePolicy.SetCacheability(HttpCacheability.Public); cachePolicy.VaryByParams["d"] = true; cachePolicy.SetOmitVaryStar(true); cachePolicy.SetExpires(now + TimeSpan.FromDays(365)); cachePolicy.SetValidUntilExpires(true); cachePolicy.SetLastModified(now); }
/// <summary> /// Caches the specified instance. /// </summary> /// <param name="instance">The instance.</param> /// <param name="duration">The duration.</param> public static void Cache(this HttpContextBase instance, TimeSpan duration) { HttpCachePolicyBase cache = instance.Response.Cache; cache.SetCacheability(HttpCacheability.Public); cache.SetOmitVaryStar(true); cache.SetExpires(instance.Timestamp.Add(duration)); cache.SetMaxAge(duration); cache.SetValidUntilExpires(true); cache.SetLastModified(instance.Timestamp); cache.SetLastModifiedFromFileDependencies(); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); }
public override void Process(BundleContext context, BundleResponse bundleResponse) { HttpCachePolicyBase cachePolicy = context.HttpContext.Response.Cache; cachePolicy.SetCacheability(HttpCacheability.Public); cachePolicy.SetOmitVaryStar(true); cachePolicy.SetExpires(DateTime.Now.AddDays(3)); cachePolicy.SetValidUntilExpires(true); cachePolicy.SetLastModified(DateTime.Now); base.Process(context, bundleResponse); /* cachePolicy.VaryByHeaders["User-Agent"] = true; */ }
/// <summary> /// Set the caching policy. /// </summary> /// <param name="context">The context.</param> /// <param name="duration">The duration.</param> public void Cache(HttpContextBase context, TimeSpan duration) { if ((duration > TimeSpan.Zero) || !context.IsDebuggingEnabled) // Do not cache in debug mode. { HttpCachePolicyBase cache = context.Response.Cache; cache.SetCacheability(HttpCacheability.Public); cache.SetOmitVaryStar(true); cache.SetExpires(context.Timestamp.Add(duration)); cache.SetMaxAge(duration); cache.SetValidUntilExpires(true); cache.SetLastModified(context.Timestamp); cache.SetLastModifiedFromFileDependencies(); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); } }
protected void SendOuptToClient(HttpContextBase context) { HttpResponseBase response = context.Response; HttpCachePolicyBase cache = context.Response.Cache; cache.SetETag(_etagCacheKey); cache.SetLastModified(_lastModify); cache.SetExpires(NetworkTime.Now.AddDays(_durationInDays)); // For HTTP 1.0 browsers cache.SetOmitVaryStar(true); cache.SetMaxAge(TimeSpan.FromDays(_durationInDays)); //cache.SetLastModified(NetworkTime.Now); cache.SetValidUntilExpires(true); cache.SetCacheability(HttpCacheability.Public); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); cache.VaryByHeaders["Accept-Encoding"] = true;// Tell proxy to cache different versions depending on Accept-Encoding response.ContentType = "application/json"; if (response.IsClientConnected) { response.Flush(); } }
public void Index(string name, string version, string condition) { HttpResponseBase response = Response; WebResourcesSection section = ConfigurationManager.GetSection(Kooboo.Common.Web.AreaHelpers.GetAreaName(this.RouteData)); if (section == null) { throw new HttpException(500, "Unable to find the web resource configuration."); } ReferenceElement settings = section.References[name]; if (settings == null) { throw new HttpException(500, string.Format("Unable to find any matching web resource settings for {0}.", name)); } Condition conditionInfo = new Condition { If = condition ?? string.Empty }; // filter the files based on the condition (Action / If) passed in IList <FileInfoElement> files = new List <FileInfoElement>(); foreach (FileInfoElement fileInfo in settings.Files) { if (fileInfo.If.Equals(conditionInfo.If)) { files.Add(fileInfo); } } // Ooutput Type response.ContentType = settings.MimeType; Stream output = response.OutputStream; // Compress if (section.Compress) { string acceptEncoding = Request.Headers["Accept-Encoding"]; if (!string.IsNullOrEmpty(acceptEncoding)) { acceptEncoding = acceptEncoding.ToLowerInvariant(); if (acceptEncoding.Contains("gzip")) { response.AddHeader("Content-encoding", "gzip"); output = new GZipStream(output, CompressionMode.Compress); } else if (acceptEncoding.Contains("deflate")) { response.AddHeader("Content-encoding", "deflate"); output = new DeflateStream(output, CompressionMode.Compress); } } } // Combine using (StreamWriter sw = new StreamWriter(output)) { foreach (FileInfoElement fileInfo in files) { string content; var dynamicResource = DynamicClientResourceFactory.Default.ResolveProvider(fileInfo.Filename); if (dynamicResource != null) { content = dynamicResource.Parse(fileInfo.Filename); } else { content = System.IO.File.ReadAllText(Server.MapPath(fileInfo.Filename)); } switch (settings.MimeType) { case "text/css": content = CSSMinify.Minify(Url, fileInfo.Filename, Request.Url.AbsolutePath, content); break; case "text/x-javascript": case "text/javascript": case "text/ecmascript": if (section.Compact && fileInfo.Compact) { content = JSMinify.Minify(content); } break; } sw.WriteLine(content.Trim()); } } // Cache if (section.CacheDuration > 0) { DateTime timestamp = HttpContext.Timestamp; HttpCachePolicyBase cache = response.Cache; int duration = section.CacheDuration; cache.SetCacheability(HttpCacheability.Public); cache.SetExpires(timestamp.AddSeconds(duration)); cache.SetMaxAge(new TimeSpan(0, 0, duration)); cache.SetValidUntilExpires(true); cache.SetLastModified(timestamp); cache.VaryByHeaders["Accept-Encoding"] = true; cache.VaryByParams["name"] = true; cache.VaryByParams["version"] = true; cache.VaryByParams["display"] = true; cache.VaryByParams["condition"] = true; cache.SetOmitVaryStar(true); } }