<%@ OutputCache Duration="15" VaryByParam="None" %>
protected void Page_Load(object sender, EventArgs e) { Thread.Sleep(10000); Response.Write("this page was generated and cache at:" + DateTime.Now.ToString()); }
属性 | 值 | 说明 |
DiskCacheable | true/false | 指定可以将输出写入基于磁盘的缓存。 |
NoStore | true/false | 指定是否发送"no store"缓存控制头。 |
CacheProfile | String name | 要存储在 web.config 中的缓存配置文件的名称。 |
VaryByParam |
None
*
Param-name |
分号分隔的字符串列表指定 GET 请求中的查询字符串值或 POST 请求中的变量。 |
VaryByHeader |
*
Header names
|
以分号分隔的字符串列表指定可能由客户端提交的标头。 |
VaryByCustom |
Browser
Custom string
|
告诉 ASP.NET 根据浏览器名称和版本或自定义字符串来改变输出缓存。 |
Location |
Any
Client
Downstream
Server
None
|
Any:页面可以缓存在任何地方。
Client:缓存内容保留在浏览器中。
Downstream:缓存内容存储在下游和服务器中。
Server:缓存内容仅保存在服务器上。
None:禁用缓存。
|
Duration | Number | 页面或控件被缓存的秒数。 |
protected void btnmagic_Click(object sender, EventArgs e) { Response.Write("<br><br>"); Response.Write("<h2> Hello, " + this.txtname.Text + "</h2>"); }
<%@ OutputCache Duration="60" VaryByParam="txtname" %>
<asp:Label ID="lbltime" runat="server"></asp:Label>
protected void Page_Load(object sender, EventArgs e) { lbltime.Text = String.Format("Page posted at: {0}", DateTime.Now.ToLongTimeString()); }
<asp:SqlDataSource ID = "SqlDataSource1" runat = "server" ConnectionString = "<%$ ConnectionStrings: ASPDotNetStepByStepConnectionString %>" ProviderName = "<%$ ConnectionStrings: ASPDotNetStepByStepConnectionString.ProviderName %>" SelectCommand = "SELECT * FROM [DotNetReferences]" EnableCaching = "true" CacheDuration = "60"> </asp:SqlDataSource>
Cache["key"] = item;
过载 | 说明 |
Cache.Insert((key, value); | 使用具有默认优先级和过期时间的键名和值将项插入缓存。 |
Cache.Insert(key, value, dependencies); | 使用键、值、默认优先级、过期时间和链接到其他文件或项目的 CacheDependency 名称将项目插入缓存,以便在这些更改时缓存项目不再有效。 |
Cache.Insert(key、value、dependencies、absoluteExpiration、slidingExpiration); | 这表示到期政策以及上述问题。 |
Cache.Insert(key、value、dependencies、absoluteExpiration、slidingExpiration、priority、onRemoveCallback); | 这与参数一起还允许您为缓存项和一个委托设置优先级,该委托指向要在删除项时调用的方法。 |
Cache.Insert("my_item", obj, null, DateTime.MaxValue, TimeSpan.FromMinutes(10));
protected void Page_Load(object sender, EventArgs e) { if (this.IsPostBack) { lblinfo.Text += "Page Posted Back.<br/>"; } else { lblinfo.Text += "page Created.<br/>"; } if (Cache["testitem"] == null) { lblinfo.Text += "Creating test item.<br/>"; DateTime testItem = DateTime.Now; lblinfo.Text += "Storing test item in cache "; lblinfo.Text += "for 30 seconds.<br/>"; Cache.Insert("testitem", testItem, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero); } else { lblinfo.Text += "Retrieving test item.<br/>"; DateTime testItem = (DateTime)Cache["testitem"]; lblinfo.Text += "Test item is: " + testItem.ToString(); lblinfo.Text += "<br/>"; } lblinfo.Text += "<br/>"; }
Page Created.
Creating test item.
Storing test item in cache for 30 seconds.
Page Posted Back. Retrieving test item. Test item is: 14-07-2010 01:25:04