如何?asp.net 2.0的SqlCacheDependency

來源:互聯網
上載者:User
asp.net|cache|sql

以前使用Cache的時候,總是當心Cache中資料版本和資料庫中資料版本不一致,雖然通過觸發器+檔案IO實現的檔案快取依賴也可以實現快取資料的及時更新,但是每個應用產生一個IO檔案,不怎麼經濟,另外觸發器效能也不是很高,所以經常在添加刪除修改的時候,刪除緩衝索引值來實現,但可能還有遺漏的地方。asp.net 2.0中實現了SqlCacheDependency,本文描述如何在asp.net 2.0實現SqlCacheDependency:
首先,在web.config system.web節點檔案裡面加上如下代碼:


   <caching>
    <sqlCacheDependency enabled="true">
     <databases>
      <add name="AtlasDemoDb" connectionStringName="AtlasDemoDbConnectionString" pollTime="12000"></add>
     </databases>
    </sqlCacheDependency>
   </caching>

這樣即配置了一個緩衝依賴的資料庫,在頁面中的ObjectDataSource對象的EnableCaching設定為true.,然後設定SqlCacheDependency屬性為AtlasDemoDb:SimpleDemo1,AtlasDemoDb為web.config配置的快取資料庫名稱,SimpleDemo1為該資料庫下的一個表名。
光是這樣,系統還會出現如下問題:


沒有為 SQL 快取通知啟用資料庫“AtlasDemoDb”。

要為 SQL 快取通知啟用資料庫,請使用 System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications 方法,或命令列工具 aspnet_regsql。要使用此工具,請運行“aspnet_regsql.exe -?”以瞭解詳細資料。
按照上面所說,運行aspnet_regsql.exe 比較煩,可以在Page_Load事件中填寫


System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(System.Configuration.ConfigurationManager.ConnectionStrings["AtlasDemoDbConnectionString"].ConnectionString, "SimpleDemo1");
這樣就可以使用SqlCacheDependency了。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.