您的位置:首页 > 编程语言 > ASP

ASP.NET缓存总结

2016-11-13 22:35 253 查看
缓存的作用是为了提升应用程序的性能。ASP.NET使用两种基本的缓存机制,第一种是应用程序缓存,另一种是页输出缓存。

    配置页面级缓存

      配置页面级缓存主要用到的是 @ OutputCache 指令,Duration设置缓存时间(秒),VaryByParam设置页面参数

<!--设置页面级缓存,缓存时间5秒->
<%@ OutputCache Duration="15"  VaryByParam="none" %>


  数据缓存

    数据缓存就是利用Cache字典的key/value存储对应的缓存数据

protected void Page_Load(object sender, EventArgs e)
{
string currentData = DateTime.Now.ToString();
Response.Write("第一次输出时间:" + currentData + "</br>");

if (Cache["data1"] == null)
{
Cache["data1"] = currentData;

Response.Write("data1第二次输出时间:" + Cache["data1"] + "</br>");
}
else
{
Response.Write("data1缓存时间1:" + Cache["data1"] + "</br>");
}
if (Cache["data2"] == null)
{
//key,value,依赖项,缓存时间,对过期时间或滑动过期时间
Cache.Insert("data2", currentData, null, DateTime.Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration);
Response.Write("data2第二次输出时间:" + Cache["data2"] + "</br>");
}
else
{
Response.Write("data2缓存时间2:" + Cache["data2"] + "</br>");
}
}

   控件缓存

控件缓存分为两种类型,一种是数据源控件缓存,还有一种是普通控件缓存

<!--数据源控件缓存->
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnableCaching="True" CacheDuration="10" CacheExpirationPolicy="Absolute"></asp:ObjectDataSource>

<!--普通控件缓存,VaryByControl的参数为要缓存的控件id-->
<%@ OutputCache Duration="10" VaryByControl="TextBox1"%>

  缓存依赖

缓存依赖项使缓存依赖于其他资源,当依赖项更改时,缓存条目项将自动从缓存中移除。缓存依赖项可以是应用程序的 Cache 中的文件、目录或与其他对象的键。如果文件或目录更改,缓存就会过期。

protected void Page_Load(object sender, EventArgs e)
{
string content = string.Empty;
if (Cache["Content"]==null)
{
content = System.IO.File.ReadAllText(Server.MapPath("TextFile1.txt"));
System.Web.Caching.CacheDependency dp = new System.Web.Caching.CacheDependency(Server.MapPath("TextFile1.txt"));
Cache.Insert("Content", content, dp);
Response.Write("第一次缓存:"+Cache["Content"]+"</br>");

}
else
{
Response.Write("缓存文件"+Cache["Content"]+"</br>");
}
}


配置文件中设置缓存

<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="cacheprofile" duration="15"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
</system.web>

<%@ OutputCache CacheProfile="cacheprofile" VaryByParam="none" %>


缓存回调函数

protected void Page_Load(object sender, EventArgs e)
{
string str = "";
if (Cache["key"] == null)
{
str = System.IO.File.ReadAllText(Server.MapPath("TextFile1.txt")); //读取TextFile1.txt文件中的数据
CacheDependency dp = new CacheDependency(Server.MapPath("TextFile1.txt"));//建立缓存依赖项dp
Cache.Insert("key", str, dp, DateTime.Now.AddSeconds(20), Cache.NoSlidingExpiration, CacheItemPriority.Low, CacheItemRemovedCallback);
//CacheItemPriority这个参数为缓存的优先级他分好多种级别,为了防止缓存占满时系统自己删除缓存的优先顺序废除缓存的,后面的为回调函数的名称。
Response.Write(Cache["key"]);   //如果TextFile1.txt这个文件的内容不变就一直读取缓存中的数据,一旦TextFile1.txt文件中的数据改变里面重新读取TextFile1.txt文件中的数据
}
else
{
Response.Write(Cache["key"]);
}

}

public void CacheItemRemovedCallback(string key, object value, CacheItemRemovedReason reason) //这个为缓存移除时的回调函数,一定要保持与 Cache.Insert()方法中的最后一个参数名字一致,
//这里使用了委托,你可以在 Cache.Insert()这个函数中转定义看到的,所以这里的格式就只能按我写的这种方法签名写。
{
System.IO.File.WriteAllText(Server.MapPath("log.txt"),"缓存移除原因为:"+reason.ToString());
}-- [ EOF ] --
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: