首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

NBearV3 教程——快取篇

     編輯:關於.NET

版本資訊

版本:1.0 [2006-11-18]

簡介

本教程介紹 NBearV3 內建的查詢快取支援功能。

學習目標

透過本教程,讀者應能全面掌握 NBearV3 查詢快取的配置與使用。

程式碼範例

本教程不包含演示代碼。

預估時間

不超過 10 分鐘。


1. 配置並啟用快取功能

需在 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>

關鍵點:

  1. 在 configSections 中註冊 cacheConfig 區段。
  2. 添加 cacheConfig 區段,enable="true" 啟用快取。
  3. cachingTables 中可包含多個 add 元素:
    • key 格式為 AAA.BBB:AAA 為連線字串的 name(如 Northwind),BBB 為資料庫實際表名、視圖名或預存程序名。
    • value 為快取有效時間(秒)。

2. 代碼中啟用、停用與使用自動快取

配置完成後,所有 NBear.Data.Gateway 實例預設開啟自動快取。查詢被設定為需快取的表、視圖或預存程序時,會自動快取。

可呼叫 Gateway.TurnOnCache() 與 TurnOffCache() 控制單一 Gateway 的快取支援。

自動快取失效的兩種情況

  1. 快取資料超過設定的過期時間。
  2. 執行了該實體的強類型寫操作(Gateway.Delete/Save/BatchDelete/BatchUpdate 等帶泛型實體類型參數的方法)。

註:所有快取資料作為 Gateway 的靜態變數,對所有 Gateway 實例可見且共享。即使有多個 Gateway 實例,它們共享所有快取資料。甚至禁用快取的 Gateway 執行強類型寫入操作,也會導致全域 Gateway 快取中對應實體的快取資料過期。

3. 代碼中使用自定義快取

除預設自動快取外,亦可手動使用自定義快取。任意時候皆可透過任何 Gateway 實例的 AddCache()、GetCache()、RemoveCache() 與 RemoveCaches() 方法手動讀寫、刪除快取物件。


正文結束

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved