Scalar

态度决定一切,正能量走起!

导航

Asp.net缓存

1、页面缓存

在页面头部加上

<!--
页面缓存Duration="10"单位是秒 VaryByParam="*"只要页面有发生变化则重新创建缓存,否则从缓存中获取值

-->

<%@ OutputCache Duration="10" VaryByParam="*" %>

 

<!--
为相同缓存页面创建相同缓存,在Web.config中的<system.web>节点下配置缓存

CacheProfile="myPage"取至Web.config的配置文件
-->

<caching>
  <outputCache enableOutputCache="true"/>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="myPage" duration="10" enabled="true"/>
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

<%@ OutputCache CacheProfile="myPage" VaryByParam="id" %>

2、文件缓存依赖(根据文件myCacheDenpency.htm是否有变化来创建缓存)

引用头文件

using System.Web.Caching;

private void GetDateTime()
{
  //键
  string key="TimeNow";
  //试图从缓存中获取时间
  string timeStr = (string)HttpRuntime.Cache[key];
  if (string.IsNullOrEmpty(timeStr))
  {
    //如果获取失败

    //重新获取当前时间
    timeStr = DateTime.Now.ToString();
    //新建缓存依赖
    CacheDependency dep = new CacheDependency(Server.MapPath("~/myCacheDenpency.htm"),DateTime.Now);
    //插入缓存
    Cache.Insert(key, timeStr, dep);
  }
  //显示时间
  Label1.Text = timeStr;
}

posted on 2011-12-15 15:30  Scalar  阅读(138)  评论(0)    收藏  举报