一次我们网站的web性能优化

时间:2023-03-10 04:10:45
一次我们网站的web性能优化

1.Google的Web优化最佳实践

利用PageSpeed工具 对我们红酒世界网进行检测时,发现了下面的几个问题

1.Leverage browser caching

    1.1.通过web.config设置静态文件缓存

           <system.webServer>

           <staticContent>

        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
       </staticContent>

              </system.webServer>

  这里设置了1天的静态文件缓存。

1.2.利用代码处理动态文件缓存

在需要进行动态文件缓存的时候调用,也可以写在basepage下, 关于http客户端缓存可以参考:http://www.cnblogs.com/luminji/archive/2011/09/14/2174751.html

 private void SetBuffer()
{
if (HttpContext.Current != null)
{ var modifiedTime = DateTime.MinValue;
if (Request.Headers["If-Modified-Since"] != null && DateTime.TryParse(Request.Headers["If-Modified-Since"], out modifiedTime))
{
if (TimeSpan.FromTicks(DateTime.Now.Ticks - modifiedTime.Ticks).TotalSeconds < secondsTime)
{
Response.ClearContent();
Response.StatusCode = (int)System.Net.HttpStatusCode.NotModified;
Response.SuppressContent = true;
return;
}
}
else
{
//设置客户端缓存状态
SetClientCaching(Response, DateTime.Now);
}
}
}
/// 设置客户端缓存状态
/// </summary>
/// <param name="response"></param>
/// <param name="lastModified"></param>
private void SetClientCaching(HttpResponse response, DateTime lastModified)
{
response.Cache.SetETag(lastModified.Ticks.ToString());
response.Cache.SetLastModified(lastModified);
//public 以指定响应能由客户端和共享(代理)缓存进行缓存。
response.Cache.SetCacheability(HttpCacheability.Public);
//是允许文档在被视为陈旧之前存在的最长绝对时间。
response.Cache.SetMaxAge(new TimeSpan(0, 0, 0, 30));
//将缓存过期从绝对时间设置为可调时间
response.Cache.SetSlidingExpiration(true);
}