缓存是程序提高性能最直接有效的手段
页面缓存,文件缓存依赖,数据库缓存依赖
一.页面缓存://.net自己帮我们实现,我们只负责进行配置
配置1:<%@ OutputCache Duration="" VaryByParam="none" %>//Duration时间单位是秒,VaryByParam多个以;分隔开。
配置2:<%@ OutputCache CacheProfile="myPageCache" VaryByParam="none" %>
<caching>
<outputCache enableOutputCache="true"/>
<outputCacheSettings>
<outputCacheProfiles>
<add name="myPageCache" duration="" enabled="true"/>
<outputCacheProfiles>
</outputCacheSettings>
</caching>
二.文件缓存依赖://需要我们自己加入到缓存中
//键
String key = "TimeNow";
//试图从缓存中获取时间
string strTime = HttpRuntime.Cache[key];
if(string.IsNullOrEmpty(strTime))
{
strTime = DateTime.Now.ToString();
CacheDependency dep = new CacheDependency(Server.MapPath("dep.txt"));
Cache.Insert(key,strTime,dep);
}
//使用strTime
三.数据库缓存依赖
生成表(AspNet_SqlCacheTablesChangeNotification)及存储过程:c:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
实例:aspnet_regsql.exe –S localhost –E –d CacheTest –ed(为SQL缓存依赖启用该数据库)
说明:-S服务器; –E Windows身份验证;-d 数据库名称;
aspnet_regsql.exe –S localhost – E –d CacheTest –t Student –et(为SQL缓存依赖项启用该表)
AggregateCacheDependency dependency = new AggregateCacheDependency();
string dbName = ConfigurationManger.AppSettings["dbName"];
String tbStr str = ConfigurationManger.AppSettings["tbName"];
Foreach(string table in tables)
{
dependency.add(new SqlCacheDependency(dbName,table));
}