.Net 2.0 緩衝使用

來源:互聯網
上載者:User

     在訪問量大,但更新較少的網站中使用緩衝,可以大大提高運行效率;加上.NET 2.0提供的緩衝依賴機制,我們可以很方便的對緩衝進行管理更新;以下是本人學習的一點心得體會,希望能夠起到拋磚引玉的作用。

建立緩衝依賴:

 

        /**//// <summary>
        /// 建立緩衝依賴項
        /// </summary>
        /// <returns></returns>
        private AggregateCacheDependency TableDependency()
        {
            AggregateCacheDependency dependency = new AggregateCacheDependency();
            dependency.Add(new SqlCacheDependency("MSPetShop4", "表名稱"));

            return dependency;
        }

 

一個非常簡單的方法,首先我們先看看兩個.NET 2.0新增的兩個類:

AggregateCacheDependency在System.Web.Caching命名空間中,AggregateCacheDependency主要作用是用於組合 ASP.NET 應用程式的 Cache 對象中儲存的項和 CacheDependency 對象的數組之間的多個依賴項。

SqlCacheDependency也存在於System.Web.Caching命名空間中,這個類用於建立ASP.NET應用程式的Cache對象中儲存的項和特定SQL Server資料庫表之間的聯絡。

SqlCacheDependency是如何建立Cache對象中儲存的項和特定SQL Server資料庫表之間的聯絡的呢?看一下Web.Config設定檔就一目瞭然了。

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
 <connectionStrings>
  <add name="LocalConnString" connectionString="Server=(Local);uid=sa;pwd=123456;DataBase=MSPetShop4"/>
 </connectionStrings>
 <system.web>
  <caching>
   <sqlCacheDependency enabled="true" pollTime="10000">
    <databases>
     <add name="MSPetShop4" connectionStringName="LocalConnString" pollTime="10000"/>
    </databases>
   </sqlCacheDependency>
  </caching>
  <compilation debug="true"/>
        </system.web>
</configuration>

配置節<databases><add name="MSPetShop4" connectionStringName="LocalConnString" pollTime="10000"/></databases>中配置了資料庫資訊,SqlCacheDependency類會自動完成對此配置節資訊的讀取以建立和資料庫之間的聯絡。(注意)name="MSPetShop4"必須和new

SqlCacheDependency("MSPetShop4", "表名稱")中的資料庫名稱相一致。更多的配置資訊可以查看(MSDN協助文檔)。

使資料庫支援SqlCacheDependency特性:

要使得7.0或者2000版本的SQL Server支援SqlCacheDependency特性,需要對資料庫伺服器執行相關的配置步驟。有兩種方法配置SQL Server:

使用aspnet_regsql命令列工具,或者使用SqlCacheDependencyAdmin類。

aspnet_regsql工具位於Windows\Microsoft.NET\Framework\[版本]檔案夾中,如果要配置SqlCacheDependency,則需要以命令列的方式執行。

以下是該工具的命令參數說明:
-?  顯示該工具的協助功能;
-S  後接的參數為資料庫伺服器的名稱或者IP地址;
-U  後接的參數為資料庫的登陸使用者名稱;
-P  後接的參數為資料庫的登陸密碼;
-E  當使用windows整合驗證時,使用該功能;
-d  後接參數為對哪一個資料庫採用SqlCacheDependency功能;
-t  後接參數為對哪一個表採用SqlCacheDependency功能;
-ed  允許對資料庫使用SqlCacheDependency功能;
-dd  禁止對資料庫採用SqlCacheDependency功能;
-et  允許對資料表採用SqlCacheDependency功能;
-dt  禁止對資料表採用SqlCacheDependency功能;
-lt  列出當前資料庫中有哪些表已經採用sqlcachedependency功能。

比如在petshop4.0的資料庫中使用SqlCacheDependency特性:aspnet_regsql -S localhost -E -d MSPetShop4 -ed

以上面的命令為例,說明將對名為MSPetShop4的資料庫採用SqlCacheDependency功能,且SQL Server採用了windows整合驗證方式。我們還可以

對相關的資料表執行aspnet_regsql命令,如:
aspnet_regsql -S localhost -E -d MSPetShop4 -t Item -et
aspnet_regsql -S localhost -E -d MSPetShop4 -t Product -et
aspnet_regsql -S localhost -E -d MSPetShop4 -t Category -et

最後為使用緩衝:
          

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string key = "TableCache"; //緩衝名稱
                DataSet data = (DataSet)HttpRuntime.Cache[key]; //擷取緩衝

                // 判斷快取資料為空白
                if (data == null)
                {
                    // 擷取資料
                    data = GetDataSource(); 

                    // 建立緩衝依賴
                    AggregateCacheDependency cd = TableDependency();

                    // 建立緩衝
                    HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, 

CacheItemPriority.High, null);
                }

                GridView1.DataSource = data; //綁定資料
                GridView1.DataBind();
            }
        }

 

擷取資料來源的方法,結合實際使用做修改。

 

        private DataSet GetDataSource()
        {
            string ConnectionStringLocal = ConfigurationManager.ConnectionStrings["LocalConnString"].ConnectionString;
            SqlConnection connPubs = new SqlConnection(ConnectionStringLocal);
            SqlDataAdapter dad = new SqlDataAdapter("SELECT TOP 50 * FROM Product", connPubs);
            DataSet ds = new DataSet();
            dad.Fill(ds);

            return ds;
        }

就這麼簡單:)
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.