private void SetResponseCache(HttpContext context) { HttpCachePolicy cache = context.Response.Cache; cache.SetLastModified(new DateTime(ResourceManager.GetAssemblyTime(typeof(ResourceManager).Assembly))); cache.SetOmitVaryStar(true); cache.SetVaryByCustom("v"); cache.SetExpires(DateTime.UtcNow.AddYears(1)); cache.SetMaxAge(TimeSpan.FromDays(365)); cache.SetValidUntilExpires(true); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); cache.SetCacheability(HttpCacheability.Public); }
private void SetResponseCache(HttpContext context) { HttpCachePolicy cache = context.Response.Cache; cache.SetLastModified(DateTime.Now.ToUniversalTime().AddSeconds(-1.0)); cache.SetOmitVaryStar(true); cache.SetVaryByCustom("v"); cache.SetExpires(DateTime.UtcNow.AddDays(365.0)); cache.SetMaxAge(TimeSpan.FromDays(365.0)); cache.SetValidUntilExpires(true); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); cache.SetCacheability(HttpCacheability.Public); cache.SetLastModifiedFromFileDependencies(); }
/// <summary> /// 设置浏览器缓存 /// </summary> public void SetResponseCache() { HttpCachePolicy cache = HttpCurrentContext.Response.Cache; DateTime modifiedDate = new DateTime(AssemblyManager.GetAssemblyTime(typeof(ResourceManager).Assembly).Ticks).ToUniversalTime(); DateTime nowDate = DateTime.Now.ToUniversalTime().AddSeconds(-1); if (modifiedDate > nowDate) { modifiedDate = nowDate; } cache.SetLastModified(modifiedDate); cache.SetOmitVaryStar(true); cache.SetVaryByCustom("v"); cache.SetExpires(DateTime.UtcNow.AddDays(365)); cache.SetMaxAge(TimeSpan.FromDays(365)); cache.SetValidUntilExpires(true); cache.SetRevalidation(HttpCacheRevalidation.AllCaches); cache.SetCacheability(HttpCacheability.Public); cache.SetLastModifiedFromFileDependencies(); }
/// <summary> /// Specifies a custom text string to vary cached output responses by. /// </summary> /// <param name="custom">The text string to vary cached output by. </param> /// <exception cref="T:System.ArgumentNullException"><paramref name="custom" /> is null. </exception> /// <exception cref="T:System.InvalidOperationException"> /// The /// <see cref="M:System.Web.HttpCachePolicy.SetVaryByCustom(System.String)" /> method has already been called. /// </exception> public void SetVaryByCustom(string custom) { policy.SetVaryByCustom(custom); }
public override void SetVaryByCustom(string custom) { _httpCachePolicy.SetVaryByCustom(custom); }
public void Deny_Unrestricted() { HttpCachePolicy cache = response.Cache; Assert.IsNotNull(cache.VaryByHeaders, "VaryByHeaders"); Assert.IsNotNull(cache.VaryByParams, "VaryByParams"); cache.AddValidationCallback(new HttpCacheValidateHandler(Validate), null); cache.AppendCacheExtension("mono"); cache.SetCacheability(HttpCacheability.NoCache); cache.SetCacheability(HttpCacheability.NoCache, "mono"); cache.SetETag("etag"); try { cache.SetETagFromFileDependencies(); } catch (TypeInitializationException) { // 1.1 tries to initialize HttpRuntime } catch (InvalidOperationException) { // expected } cache.SetExpires(DateTime.MinValue); cache.SetLastModified(DateTime.Now); try { cache.SetLastModifiedFromFileDependencies(); } catch (InvalidOperationException) { // expected } catch (NotImplementedException) { // mono } cache.SetMaxAge(TimeSpan.FromTicks(1000)); try { cache.SetNoServerCaching(); } catch (NotImplementedException) { // mono } try { cache.SetNoStore(); } catch (NotImplementedException) { // mono } try { cache.SetNoTransforms(); } catch (NotImplementedException) { // mono } cache.SetProxyMaxAge(TimeSpan.FromTicks(2000)); cache.SetRevalidation(HttpCacheRevalidation.None); cache.SetSlidingExpiration(true); try { cache.SetValidUntilExpires(true); } catch (NotImplementedException) { // mono } cache.SetVaryByCustom("custom"); cache.SetAllowResponseInBrowserHistory(true); #if NET_2_0 try { cache.SetOmitVaryStar(false); } catch (NotImplementedException) { // mono } #endif }
void SetCacheFromCacheProfile() { HttpCachePolicy cache = HttpContext.Current.Response.Cache; if (this.cacheProfile.NoStore) { cache.SetNoStore(); } // Location is not required to be set in the config. The default is Any, // but if it is not set in the config the value will be -1. So must correct for this. if ((int)(this.cacheProfile.Location) == -1) { cache.SetCacheability(HttpCacheability.Public); } else { switch (this.cacheProfile.Location) { case OutputCacheLocation.Any: cache.SetCacheability(HttpCacheability.Public); break; case OutputCacheLocation.Client: cache.SetCacheability(HttpCacheability.Private); break; case OutputCacheLocation.Downstream: cache.SetCacheability(HttpCacheability.Public); cache.SetNoServerCaching(); break; case OutputCacheLocation.None: cache.SetCacheability(HttpCacheability.NoCache); break; case OutputCacheLocation.Server: cache.SetCacheability(HttpCacheability.ServerAndNoCache); break; case OutputCacheLocation.ServerAndClient: cache.SetCacheability(HttpCacheability.ServerAndPrivate); break; default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR2.GetString(SR2.CacheProfileLocationNotSupported, this.cacheProfile.Location))); } } if (this.cacheProfile.Location != OutputCacheLocation.None) { cache.SetExpires(HttpContext.Current.Timestamp.AddSeconds((double)this.cacheProfile.Duration)); cache.SetMaxAge(new TimeSpan(0, 0, this.cacheProfile.Duration)); cache.SetValidUntilExpires(true); cache.SetLastModified(HttpContext.Current.Timestamp); if (this.cacheProfile.Location != OutputCacheLocation.Client) { if (!string.IsNullOrEmpty(this.cacheProfile.VaryByContentEncoding)) { foreach (string contentEncoding in this.cacheProfile.VaryByContentEncoding.Split(seperatorChar)) { cache.VaryByContentEncodings[contentEncoding.Trim()] = true; } } if (!string.IsNullOrEmpty(this.cacheProfile.VaryByHeader)) { foreach (string header in this.cacheProfile.VaryByHeader.Split(seperatorChar)) { cache.VaryByHeaders[header.Trim()] = true; } } if (this.cacheProfile.Location != OutputCacheLocation.Downstream) { if (!string.IsNullOrEmpty(this.cacheProfile.VaryByCustom)) { cache.SetVaryByCustom(this.cacheProfile.VaryByCustom); } if (!string.IsNullOrEmpty(this.cacheProfile.VaryByParam)) { foreach (string parameter in cacheProfile.VaryByParam.Split(seperatorChar)) { cache.VaryByParams[parameter.Trim()] = true; } } if (!string.IsNullOrEmpty(this.cacheProfile.SqlDependency)) { CacheDependency cacheDependency = this.CreateSingleCacheDependency(cacheProfile.SqlDependency); HttpContext.Current.Response.AddCacheDependency(new CacheDependency[] { cacheDependency }); } } } } }
/// <summary> /// Configures ASP.Net's Cache policy based on properties set /// </summary> /// <param name="policy">cache policy to set</param> void ICachePolicyConfigurer.Configure(HttpCachePolicy policy) { policy.SetAllowResponseInBrowserHistory(allowInHistory); policy.SetCacheability(cacheability); policy.SetOmitVaryStar(omitVaryStar); policy.SetRevalidation(revalidation); policy.SetSlidingExpiration(slidingExpiration); policy.SetValidUntilExpires(validUntilExpires); if (duration != 0) { policy.SetExpires(DateTime.Now.AddSeconds(duration)); } if (varyByContentEncodings != null) { foreach (var header in varyByContentEncodings.Split(',')) { policy.VaryByContentEncodings[header.Trim()] = true; } } if (varyByCustom != null) { policy.SetVaryByCustom(varyByCustom); } if (varyByHeaders != null) { foreach (var header in varyByHeaders.Split(',')) { policy.VaryByHeaders[header.Trim()] = true; } } if (varyByParams != null) { foreach (var param in varyByParams.Split(',')) { policy.VaryByParams[param.Trim()] = true; } } if (cacheExtension != null) { policy.AppendCacheExtension(cacheExtension); } if (setEtagFromFileDependencies) { policy.SetETagFromFileDependencies(); } if (setLastModifiedFromFileDependencies) { policy.SetLastModifiedFromFileDependencies(); } if (setNoServerCaching) { policy.SetNoServerCaching(); } if (setNoStore) { policy.SetNoStore(); } if (setNoTransforms) { policy.SetNoTransforms(); } if (etag != null) { policy.SetETag(etag); } if (isLastModifiedSet) { policy.SetLastModified(lastModified); } if (isMaxAgeSet) { policy.SetMaxAge(TimeSpan.FromSeconds(maxAge)); } if (isProxyMaxAgeSet) { policy.SetProxyMaxAge(TimeSpan.FromSeconds(proxyMaxAge)); } }
public override void SetVaryByCustom(string custom) { _policy.SetVaryByCustom(custom); }