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); } }
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 void HandleCaching(HttpCachePolicyBase cache, string etag) { cache.SetCacheability(System.Web.HttpCacheability.Public); //cache.SetExpires(DateTime.Now.AddDays(7)); cache.SetMaxAge(TimeSpan.FromDays(7)); cache.SetETag(etag); //cache.SetValidUntilExpires(false); cache.VaryByParams["id"] = true; cache.VaryByParams["size"] = true; //cache.SetLastModified(DateTime.SpecifyKind(picture.UpdatedOnUtc, DateTimeKind.Utc)); }
protected override void SendMediaHeaders(Media media, HttpContextBase context) { GetResponseCacheHeadersArgs args = new GetResponseCacheHeadersArgs(media.MediaData.MediaItem.InnerItem, new ResponseCacheHeaders() { Vary = this.GetVaryHeader(media, context) }) { RequestType = new RequestTypes?(RequestTypes.Media) }; GetResponseCacheHeadersPipeline.Run(args); HttpCachePolicyBase cache = context.Response.Cache; if (args.CacheHeaders.LastModifiedDate.HasValue) { cache.SetLastModified(args.CacheHeaders.LastModifiedDate.Value); } if (!string.IsNullOrEmpty(args.CacheHeaders.ETag)) { cache.SetETag(args.CacheHeaders.ETag); } if (args.CacheHeaders.Cacheability.HasValue) { cache.SetCacheability(args.CacheHeaders.Cacheability.Value); } if (args.CacheHeaders.MaxAge.HasValue) { cache.SetMaxAge(args.CacheHeaders.MaxAge.Value); } if (args.CacheHeaders.ExpirationDate.HasValue) { cache.SetExpires(args.CacheHeaders.ExpirationDate.Value); } if (!string.IsNullOrEmpty(args.CacheHeaders.CacheExtension)) { cache.AppendCacheExtension(args.CacheHeaders.CacheExtension); } if (string.IsNullOrEmpty(args.CacheHeaders.Vary)) { return; } context.Response.AppendHeader("vary", args.CacheHeaders.Vary); }
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 static void Apply(this HttpCacheOptions options, HttpCachePolicyBase policy) { if (policy == null) { return; } policy.SetCacheability(options.IsPublic ? HttpCacheability.Public : HttpCacheability.Private); if (options.LastModified.HasValue) { policy.SetLastModified(options.LastModified.Value); } if (!string.IsNullOrEmpty(options.ETag)) { policy.SetETag(options.QuotedETag); } if (options.MaxAge.HasValue) { policy.SetMaxAge(options.MaxAge.Value); } }
public void ProcessRequest(HttpContextBase context) { this._context = context; HttpRequestBase request = context.Request; HttpResponseBase response = context.Response; Uri url = request.Url; string str = !(url == (Uri)null) ? url.LocalPath : throw new HttpException(500, BundleTransformer.Core.Resources.Strings.Common_ValueIsNull); if (string.IsNullOrWhiteSpace(str)) { throw new HttpException(500, BundleTransformer.Core.Resources.Strings.Common_ValueIsEmpty); } if (!this._virtualFileSystemWrapper.FileExists(str)) { throw new HttpException(404, string.Format(BundleTransformer.Core.Resources.Strings.Common_FileNotExist, (object)str)); } string bundleVirtualPath = request.QueryString[Common.BundleVirtualPathQueryStringParameterName]; if (string.IsNullOrWhiteSpace(bundleVirtualPath) && this.IsStaticAsset) { AssetHandlerBase.ProcessStaticAssetRequest(context); } else { string processedAssetContent; try { processedAssetContent = this.GetProcessedAssetContent(str, bundleVirtualPath); } catch (HttpException ex) { throw; } catch (AssetTranslationException ex) { throw new HttpException(500, ex.Message, (Exception)ex); } catch (AssetPostProcessingException ex) { throw new HttpException(500, ex.Message, (Exception)ex); } catch (FileNotFoundException ex) { throw new HttpException(500, string.Format(BundleTransformer.Core.Resources.Strings.AssetHandler_DependencyNotFound, (object)ex.Message, (object)ex)); } catch (Exception ex) { throw new HttpException(500, string.Format(BundleTransformer.Core.Resources.Strings.AssetHandler_UnknownError, (object)ex.Message, (object)ex)); } HttpCachePolicyBase cache = response.Cache; if (this._assetHandlerConfig.DisableClientCache) { response.StatusCode = 200; response.StatusDescription = "OK"; cache.SetCacheability(HttpCacheability.NoCache); cache.SetExpires(DateTime.UtcNow.AddYears(-1)); cache.SetValidUntilExpires(false); cache.SetNoStore(); cache.SetNoServerCaching(); response.ContentType = this.ContentType; response.Write(processedAssetContent); } else { string assetEtag = AssetHandlerBase.GenerateAssetETag(processedAssetContent); int num = AssetHandlerBase.IsETagHeaderChanged(request, assetEtag) ? 1 : 0; if (num != 0) { response.StatusCode = 200; response.StatusDescription = "OK"; } else { response.StatusCode = 304; response.StatusDescription = "Not Modified"; response.AddHeader("Content-Length", "0"); } response.AddHeader("X-Asset-Transformation-Powered-By", "Bundle Transformer"); cache.SetCacheability(HttpCacheability.Public); cache.SetExpires(DateTime.UtcNow.AddYears(-1)); cache.SetValidUntilExpires(true); cache.AppendCacheExtension("must-revalidate"); cache.SetNoServerCaching(); cache.VaryByHeaders["If-None-Match"] = true; cache.SetETag(assetEtag); if (num != 0) { response.ContentType = this.ContentType; response.Write(processedAssetContent); } } context.ApplicationInstance.CompleteRequest(); } }
/// <summary> /// 设置内容标识 /// </summary> /// <param name="etag">用于标识内容的哈希值</param> public override void SetETag(string etag) { _cachePolicy.SetETag(etag); }
void SetLongLivedCacheHeaders(HttpCachePolicyBase cache, string serverETag) { cache.SetCacheability(HttpCacheability.Public); cache.SetETag(serverETag); cache.SetExpires(DateTime.UtcNow.AddYears(1)); }