第五章 设计程序架构 之 设计缓存策略

1. 概述

  本章内容包括:如何实现 页面输出缓存、数据缓存、程序缓存 以及 HTTP缓存。

2. 主要内容

  2.1 页面输出缓存

    缓存是开发高可用web程序的重要部分。

    ① 浏览器可以以预定义的时长保存一个HTTP GET请求。

[OutputCache(Duration=120, VaryByParam="Name", Location="ServerAndClient")]  
Public ActionResult Index() 
{ 
    Return View("Index",myData);  
}

    ② Donut缓存

      Donut缓存是一种服务端技术,可以实现在缓存页面中保留一个动态的部分(Donut holes)。

      * ASP.NET Web forms 用 substitution控件来实现Donut缓存的功能。构建在其上的ASP.NET MVC4当然也可以使用。

    ③ Donut hole 缓存

      与Donut相反,Donut hole 缓存页面的指定区域。

      * ASP.NET MVC中通过child actions来支持Donut hole。

[ChildActionOnly]  
[OutputCache(Duration=60)]  
public ActionResult ProductsChildAction() 
{ 
    // Fetch products from the database and 
    // pass it to the child view via its ViewBag 
    ViewBag.Products = Model.GetProducts(); 
 
    return View(); 
}

        *也可以把这个缓存属性应用到控件上。

      ④ 分布式缓存

        分布式缓存是指多服务器环境中的缓存。微软提供的一个解决方案是 Windows Server AppFabric。包含AppFabric 缓存服务。

      分布式缓存的同步问题以及信息安全问题也是需要考虑的。

     APPFabric还有一个好处就它可以使用session来保存数据。

       Windows Azure APPFabric 可以提供一个共享的缓存服务。

  2.2 数据缓存

    ① 服务端另一种形式的缓存可以用.NET4的缓存架构来实现:默认使用 System.Runtime.Caching下的ObjectCache和 MemoryCache来实现。

    一般是创建一个CacheProvider类来实现 ICacheProvider 接口。

    ② 数据缓存是减轻数据库压力和提高程序相应速度的重要方式。

  2.3 程序缓存

    HTML5定义了一组程序缓存API(AppCache),允许开发者通过其访问本地浏览器缓存。具体步骤如下:

    ① 创建程序缓存清单(manifest)。

CACHE MANIFEST 
 
    # Cached entries. 
    CACHE: 
    /favicon.ico 
    default.aspx 
    site.css 
    images/logo.jpg 
    scripts/application.js 
 
    # Resources that are "always" fetched from the server. 
    NETWORK: 
    login.asmx 
 
    FALLBACK: 
    button.png offline-button.png

    * CACHE标记的资源,在客户端缓存。

    * NETWORK标记的项,不缓存。

    * FALLBACK标记的资源,在响应资源不存在时被返回。

    ② 引用清单文件

      通过在  Layout.cshtml 或者 Master.Page 文件中的<html>标记上定义属性来引用清单文件。

<html manifest="site.manifest">

    ③ 传递清单文件

      设置MIME-type为 “text/cache-manifest”. 否则浏览器无法识别和使用清单文件。

      启用程序缓存后,浏览器只在以下三种情况下才去请求资源:

      ① 用户清空缓存后。

      ② 清单文件变动后。

      ③ 缓存被Js修改后。

  2.4 HTTP缓存

    HTTP协议包括一些对缓存有帮助的元素。Cache correctness就是其中一个。

    另一个元素是 过期模型(expiration model). HTTP协议在计算过期方面有多种规则。

    *这部分没太明白,暂时没时间细究,贴出原文:

HTTP is generally used in distributed systems, especially the Internet. The HTTP protocol in-
cludes a set of elements that are designed to help caching. Cache correctness is one of those 
elements. An HTTP server must respond to a request with the most up-to-date response held 
by the cache that is equivalent to the source server; meets the freshness case; or is an appro-
priate 304 (Not Modified), 305 (Proxy Redirect), or error (4xx or 5xx) response message. 
Another element is the expiration model that provides a server-specified or heuristic expi-
ration, and the HTTP protocol has multiple rules around calculating expiration. The protocol 
also has a validation model in which the client sends information to the server so the server 
can detect whether any changes need to be returned. Actually, the server sends a special 
status code, usually a 304 (Not Modified) response without an entity-body, when there has 
been no change in the output; otherwise, the server transmits the full response including the 
entire body. This gives the server the chance to respond with a minimal message if the valida-
tor matches; a chance to stop a full round trip requery by sending the correct information. 
With HTTP caching, everything happens automatically as part of the request stack and there 
is little programmatic impact.

3. 总结

  ① 页面输出缓存 是一种客户端和服务端之间的共享策略。包括全页缓存和局部缓存。Donut和Donut hole 是两种局部缓存方式。Donut缓存主要页面,允许局部动态的内容。Donut hole 允许主要页面动态,只缓存局部内容。

  ② 数据缓存是一种服务端技术,在业务逻辑层和数据库层实现了一个中间层。仅在缓存失效后才去重新提取数据,从而减小数据库压力,提高用户体验。

  ③ Windows APPFabric 是一种第三方工具,用于实现多个服务器间的缓存共享。

    APPFabric是构建在windows服务器之上的一组服务,用来管理分布式缓存。在MVC4程序中也可以用来管理session。

  ④ 程序缓存(Application caching)是HTML5的一个特性,可以创建缓存清单来描述网站或者页面的设置信息。

  ⑤ HTTP缓存是HTTP协议上的一个缓存方案。用来处理自己的版本的过期计算并以此来决定发送给客户端响应内容。

posted @ 2015-09-01 17:35  stone lv  阅读(377)  评论(0编辑  收藏  举报