.Net PetShop 4.0的緩衝處理

來源:互聯網
上載者:User

在訪問量非常大,但更新較少的網站中使用緩衝,可以大大提高程式啟動並執行效率,給網路使用者一個良好的體驗效果。在Microsoft提供的經典樣本項目.Net PetShop 4.0中,也提供了對緩衝的支援,本文是作者在學習此項目時的一些心得體會,有一些地方還不十分清楚,希望能夠拋磚引玉。

在.Net PetShop 4.0中,非常成功地使用了原廠模式以及介面(interface)、靜態類(Static class)、抽象類別(abstract class)等成員。在使用緩衝時,也是通過web.config配置進行設定,在使用時非常靈活。下面從底向上具體分析.Net PetShop 4.0緩衝方面的技術。

首先看一下該項目中與緩衝直接相關的命名空間:

PetShop.ICacheDependency
PetShop.TableCacheDependency
PetShop.CacheDependencyFactory
PetShop.Web

一、PetShop.ICacheDependency命名空間

最低層應該是介面的定義了,在PetShop.ICacheDependency命名空間中只定義了一個介面IPetShopCacheDependency,該介面只有一個方法 GetDependency,沒有任何參數,返回AggregateCacheDependency類型。AggregateCacheDependency是在.NET Framework 2.0 版中是新增的類,組合 ASP.NET 應用程式的 Cache 對象中儲存的項和 CacheDependency 對象的數組之間的多個依賴項(MSDN中原話)。

二、PetShop.TableCacheDependency命名空間

在PetShop.TableCacheDependency命名空間中,提供兩種類:抽象類別TableDependency和它的繼承類Category、Item和Product。抽象類別TableDependency的建構函式為:

protected TableDependency(string configKey) {
string dbName = ConfigurationManager.AppSettings["CacheDatabaseName"];
string tableConfig = ConfigurationManager.AppSettings[configKey];
string[] tables = tableConfig.Split(configurationSeparator);
foreach (string tableName in tables)
dependency.Add(new SqlCacheDependency(dbName, tableName));
}

傳遞了一個參數configKey,根據該參數從web.config檔案中擷取表名列表,同時在web.config中擷取資料庫名稱。將表名列表中的所有資料表添加到AggregateCacheDependency類型的dependency變數中。在此外使用了.NET Framework 2.0 版中是新增的另一個與緩衝有關的SqlCacheDependency類。這個類用於建立ASP.NET應用程式的Cache對象中儲存的項和特定SQL Server資料庫表之間的聯絡。AggregateCacheDependency和SqlCacheDependency都從CacheDependency繼承而來,但在.NET 2.0中還未提供Oracle等其它資料庫對應的類。

下面是web.config檔案中與緩衝相關的設定:

<!-- Cache dependency options. Possible values: PetShop.TableCacheDependency for SQL Server and keep empty for ORACLE -->
<add key="CacheDependencyAssembly" value="PetShop.TableCacheDependency"/>
<!-- CacheDatabaseName should match the name under caching section, when using TableCacheDependency -->
<add key="CacheDatabaseName" value="MSPetShop4"/>
<!-- *TableDependency lists table dependency for each instance separated by comma -->
<add key="CategoryTableDependency" value="Category"/>
<add key="ProductTableDependency" value="Product,Category"/>
<add key="ItemTableDependency" value="Product,Category,Item"/>

每個繼承類都只有一個建構函式,通過設定基類的configKey參數變成了三個不同的類。Product類的建構函式為:

public Product() : base("ProductTableDependency") { }

聯繫我們

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