我遇到缓存控制问题。我有一个IIS网站有多个主机头。当您浏览站点编号1时,将为此站点设置缓存,当您再次打开浏览器并转到第二个站点时,您将看到来自第一个站点的内容。如何根据网站用户访问次数确定缓存内容?当您有1个站点和主机标题与SAME站点相关时,一切正常。
//Set Cacheability
if (!Context.User.Identity.IsAuthenticated && _activeNode.CacheDuration > 0)
{
var eTag = GetETag(_activeNode);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
if (IsClientCached(_activeNode.UpdateTimestamp))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotModified;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.Response.End();
return;
}
var incomingEtag = HttpContext.Current.Request.Headers["If-None-Match"];
if (String.Compare(incomingEtag, eTag) == 0)
{
HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotModified;
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.Response.End();
return;
}
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime().AddMinutes(_activeNode.CacheDuration));
HttpContext.Current.Response.Cache.SetMaxAge(new TimeSpan(0, _activeNode.CacheDuration, 0));
HttpContext.Current.Response.Cache.SetLastModified(_activeNode.UpdateTimestamp);
HttpContext.Current.Response.Cache.SetETag(eTag);
}
/// <summary>
/// Gets the ETag.
/// </summary>
/// <param name="node">The node.</param>
/// <returns></returns>
private static string GetETag(Node node)
{
var etag = String.Format("{0}_{1}", node.Site.Id, node.Id);
return "\"" + Encryption.StringToMD5Hash(etag).Replace("-", null) + "\"";
}
/// <summary>
/// Determines whether [is client cached] [the specified content modified].
/// </summary>
/// <param name="contentModified">The content modified.</param>
/// <returns>
/// <c>true</c> if [is client cached] [the specified content modified]; otherwise, <c>false</c>.
/// </returns>
private bool IsClientCached(DateTime contentModified)
{
var header = Request.Headers["If-Modified-Since"];
if (header != null)
{
DateTime isModifiedSince;
if (DateTime.TryParse(header, out isModifiedSince))
{
return isModifiedSince > contentModified;
}
}
return false;
}
答案 0 :(得分:2)
听起来应该将主机头值添加到IsClientCached算法