当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  Asp.net cache 简述

 Asp.net cache 简述

点击次数:36 次 发布日期:2008-11-26 11:53:04 作者:源代码网
源代码网推荐      Asp.net支持三种类型的cache
源代码网推荐  
源代码网推荐  想写一个技术快速概述,可能写得太多了。技术概略的目的是以最快最简单的方式描述出技术要点,也是我希望的最有效率的知识传播方式。
源代码网推荐  
源代码网推荐  
源代码网推荐  1. 页面/控件cache
源代码网推荐  
源代码网推荐  2. 应用程序级cache
源代码网推荐  
源代码网推荐  3. 浏览器客户端cache
源代码网推荐  
源代码网推荐  
源代码网推荐  从实现方式来看,页面/控件cache和应用程序级cache都是存放在服务器内存里面的,随着内存的紧张程度,这些内容有可能在失效之前被提前删除。(cache的特性决定这些内容是可以放心得删除掉的)。浏览器客户端的cache是存放在客户端浏览器的cache里面 ,比如IE的临时文件夹就是起的cache的作用。每次用户请求一个页面的时候,浏览器会先从cache里面去查找一下有没有符合要求的还没有过期的cache内容,如果有的话就从cache里面直接读取跳过网络传输。
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  下面演示一下在Asp.net里面具体的写法:
源代码网推荐  
源代码网推荐  1. 页面/控件cache可以申明在aspx,ascx文件里面,也可以在code behind里面作出申明。
源代码网推荐  
源代码网推荐  <%@ OutputCache Duration="#ofseconds"
源代码网推荐  
源代码网推荐   Location="Any | Client | Downstream | Server | None |
源代码网推荐  
源代码网推荐   ServerAndClient "
源代码网推荐  
源代码网推荐   Shared="True | False"
源代码网推荐  
源代码网推荐   VaryByControl="controlname"
源代码网推荐  
源代码网推荐   VaryByCustom="browser | customstring"
源代码网推荐  
源代码网推荐   VaryByHeader="headers"
源代码网推荐  
源代码网推荐   VaryByParam="parametername"
源代码网推荐  
源代码网推荐   CacheProfile="cache profile name | """
源代码网推荐  
源代码网推荐   NoStore="true | false"
源代码网推荐  
源代码网推荐   SqlDependency="database/table name pair | CommandNotification"
源代码网推荐  
源代码网推荐  %>
源代码网推荐  
源代码网推荐  主要的参数是:
源代码网推荐  
源代码网推荐  Duration: cache有效的时间,单位秒
源代码网推荐  
源代码网推荐  Shared:只对控件有效,在控件可以同时应用与多个页面的时候,多个页面是否可以共享这一个cache而不用每个页面维护自己对这个控件的cache。
源代码网推荐  
源代码网推荐  VaryByControl:cache随控件的ID变化
源代码网推荐  
源代码网推荐  VaryByCustom:cache随用户自定义的一个变量来变化,该变量是在这里指定,然后应该在Global.ascx里面实现:
源代码网推荐  
源代码网推荐  public override string GetVaryByCustomString (
源代码网推荐  
源代码网推荐  HttpContext context,
源代码网推荐  
源代码网推荐  string custom
源代码网推荐  
源代码网推荐  )
源代码网推荐  
源代码网推荐  该声明的custom参数就是在OutPutcache里面申明的变量。通过在这个函数里面对不同的custom参数返回不同的字符串值来表示不同的cache内容。
源代码网推荐  
源代码网推荐  VaryByHeader, VaryByParam,CacheProfile也是设置不同的cache版本的。
源代码网推荐  
源代码网推荐  NoStore表示不允许把cache的内容写到内存以外其它的存储设备上,这是对安全性比较高的内容的需要。
源代码网推荐  
源代码网推荐  SqlDependency是和数据库相关的。
源代码网推荐  
源代码网推荐  
源代码网推荐  2. 应用程序级cache
源代码网推荐  
源代码网推荐  只可以在code behind里面获得,特点是可以实现自己需要的任何cache逻辑。
源代码网推荐  
源代码网推荐  可以通过Page.Cache获得,类的名字是System.Web.Caching.Cache
源代码网推荐  
源代码网推荐  
源代码网推荐  3. 浏览器客户端cache
源代码网推荐  
源代码网推荐  只可以在codebehind里面获得,可以通过Response.Cache来指定,这是一个HttpCachePolicy对象。
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  另外: 可以指定一个TimeSpan作为cache的时间,不用每次来折算到秒。
源代码网推荐  
源代码网推荐   public TimeSpan(long ticks);
源代码网推荐  
源代码网推荐   public TimeSpan(int hours, int minutes, int seconds);
源代码网推荐  
源代码网推荐   public TimeSpan(int days, int hours, int minutes, int seconds);
源代码网推荐  
源代码网推荐   public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
源代码网推荐  
源代码网推荐  如果要cache保留一个小时1分1秒,可以直接
源代码网推荐  
源代码网推荐  new TimeSpan(1,1,1)
源代码网推荐  
源代码网推荐  而不用先计算
源代码网推荐  
源代码网推荐  1小时1分1秒=3600+60+1=3661秒,然后在传入3661作为有效期。
源代码网推荐  http://www.cnblogs.com/ThomasNet/archive/2006/11/26/573104.html
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华