zoukankan      html  css  js  c++  java
  • 编程经验:高性能.NET WEB开发(3)控件缓存

    ASP.NET: clear user control output cache(控件缓存)

    1、Web.config

     

      <appSettings>

        <addkey="cacheKey"  value="myCacheKey1,myCacheKey2"/>

      </appSettings>

    在大量的用户控件或者调用同一个控件的时候,我们就给控件一个属性ClassId,然后WebConfigvalue的值就枚举ClassIdOK了。

     

    2、全局应用程序类Global.asax

    CanYouLib.Common.Config这个是一个自定义类方法,取Webconfig的某个key有关的value值。

    这里是必须的,如果后台没有改变(没有insert,update,delete等等事件),页面的操作都会重新请求控件的内容。

      string[] pCacheKey = CanYouLib.Common.Config.GetSetting("cacheKey").Split(',');

      for (int i = 0; i < pCacheKey.Length; i++)

      {

          HttpRuntime.Cache.Insert(pCacheKey[i], DateTime.Now);

      }

     

    3、WebUserControl(自定义控件)

    这里我把Page_Load都是写在页面,写在后台都是一样的。

    <%@ OutputCache VaryByParam="None" Duration="600"   %>

    <script runat="server">

        protected void Page_Load(object sender, EventArgs e)

        {

            String[] dependencyKey = new String[1];

            dependencyKey[0] = "myCacheKey1";//这里就是webconfigkey="cacheKey"其中的value

            BasePartialCachingControl pcc = Parent as BasePartialCachingControl;

            if(pcc != null)

             pcc.Dependency = new CacheDependency(null, dependencyKey);

        }

    </script>

     

    4、  进入引起某个key="cacheKey"value事件(数据更新等等)里面

     

    <script runat="server">

        protected void Button2_Click(object sender, EventArgs e)

        {

            Cache.Insert("myCacheKey", DateTime.Now);

        }

    </script>

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

        <title>控件缓存</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

            <uc1:WebUserControl ID="WebUserControl1" runat="server"></uc1:WebUserControl>

        </div>

        <div>

            <asp:Button ID="Button1" runat="server" Text="Cause a postback(导致回发)" />

            <asp:Button ID="Button2" runat="server" Text="Remove from cache(清除缓存)" OnClick="Button2_Click" />

        </div>

        </form>

    </body>

    </html>

     

    关键就是在相应的事件的地方,加个Cache.Insert("myCacheKey", DateTime.Now);

     

     

    相关文章: ASP.NET: clear user control output cache

  • 相关阅读:
    目标检测中roi的有关操作
    JavaScript高级程序设计读后感(一)
    手机app后台初学
    DTO数据传输对象
    数据库设计系列之四--ER图
    数据库设计系列之三
    数据库设计系列之二
    数据库设计系列之一
    Linux命令大全
    Linux登录密码修改
  • 原文地址:https://www.cnblogs.com/Gemgin/p/3136316.html
Copyright © 2011-2022 走看看