版本:1.0 [2006-11-18]
本教程介紹 NBearV3 內建的查詢快取支援功能。
透過本教程,讀者應能全面掌握 NBearV3 查詢快取的配置與使用。
本教程不包含演示代碼。
不超過 10 分鐘。
需在 Web.config 或 App.config 中添加 cacheConfig 設定區段:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="entityConfig" type="NBear.Common.EntityConfigurationSection, NBear.Common" />
<section name="cacheConfig" type="NBear.Data.CacheConfigurationSection, NBear.Data" />
</configSections>
<entityConfig>
<includes>
<add key="Sample" value="C:\Teddy\NBearV3\src\NBear.Test.UnitTests\EntityConfig.xml" />
</includes>
</entityConfig>
<cacheConfig enable="true">
<cachingTables>
<add key="Northwind.Orders" value="5" />
</cachingTables>
</cacheConfig>
<connectionStrings>
<add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/>
</connectionStrings>
</configuration>
關鍵點:
configSections 中註冊 cacheConfig 區段。cacheConfig 區段,enable="true" 啟用快取。cachingTables 中可包含多個 add 元素:
key 格式為 AAA.BBB:AAA 為連線字串的 name(如 Northwind),BBB 為資料庫實際表名、視圖名或預存程序名。value 為快取有效時間(秒)。配置完成後,所有 NBear.Data.Gateway 實例預設開啟自動快取。查詢被設定為需快取的表、視圖或預存程序時,會自動快取。
可呼叫 Gateway.TurnOnCache() 與 TurnOffCache() 控制單一 Gateway 的快取支援。
Gateway.Delete/Save/BatchDelete/BatchUpdate 等帶泛型實體類型參數的方法)。註:所有快取資料作為 Gateway 的靜態變數,對所有 Gateway 實例可見且共享。即使有多個 Gateway 實例,它們共享所有快取資料。甚至禁用快取的 Gateway 執行強類型寫入操作,也會導致全域 Gateway 快取中對應實體的快取資料過期。
除預設自動快取外,亦可手動使用自定義快取。任意時候皆可透過任何 Gateway 實例的 AddCache()、GetCache()、RemoveCache() 與 RemoveCaches() 方法手動讀寫、刪除快取物件。
正文結束