优化DOTTEXT之一:缓存配置项
最近在開發博客,于是想到了OPENSOURCE的DOTTEXT,下載了裝好后,感覺功能很完善,所以打算在其基礎上建立BLOG SITE。感謝http://www.cnblogs.com/,我裝的是CNBlogsDottext10Beta2版本。
我覺得一個BLOG SITE最重要的功能是:
1 換膚功能
2 RSS功能
3 TRACKBACK
在FIX、PERFORMANT以及LOCALIZATION過程中,感覺DOTTEXT的性能比較低,經分析后發現:
對于大量的訪問CONFIG配置文件的操作,DOTTEXT并沒有提供緩存機制。比如首頁,大約有三十次訪問BlogConfigurationSettings,而這全部都要執行以下代碼:
public static BlogConfigurationSettings Instance()??{
?return ((BlogConfigurationSettings)ConfigurationSettings.GetConfig("BlogConfigurationSettings"));
??}
類似的還有 HTTP鏈接動態生成,每次都要由 UrlReWriteHandlerFactory 對REQUEST鏈接進行分析,這個過程要遍歷HttpHandlers,而取得HttpHandlers要執行以下方法:??
public static HandlerConfiguration Instance()??{
???(UrlManager.HandlerConfiguration)ConfigurationSettings.GetConfig("HandlerConfiguration");??}
我們看到,每次訪問配置項,都要從CONFIG文件中讀取,而且每次都要進行裝箱的操作。
為了解決以上問題,我在Dottext.Framework命名空間中建立了CATCHER類來緩存配置項。這是個單例類,代碼如下:
using System;using System.Configuration;
namespace Dottext.Framework
{
?/// <summary>
?/// 作者:丁冬
?/// 時間:2006-02-24
?/// 說明:將配置項緩存
?/// </summary>
?public class Catcher
?{
??private Catcher()
??{
??} private static Configuration.BlogConfigurationSettings _BlogConfigurationSettings = null;
??/// <summary>
??/// 緩存BlogConfigurationSettings
??/// </summary>
??/// <returns></returns>
??public static Configuration.BlogConfigurationSettings GetBlogConfigurationSettings()
??{
???if(_BlogConfigurationSettings == null)
????_BlogConfigurationSettings = (Configuration.BlogConfigurationSettings)ConfigurationSettings.GetConfig("BlogConfigurationSettings"); return? _BlogConfigurationSettings;
??}
?}
}
相應的,將BlogConfigurationSettings 類的INSTANCE方法改為:
public static BlogConfigurationSettings Instance()
??{
???return Catcher.GetBlogConfigurationSettings();
??}
這樣,每次訪問Dottext.Framework.Configuration.BlogConfigurationSettings Instance(),都先去緩存中提取,如果為NULL,則再去讀配置文件。
同樣的,我在Dottext.Common.UrlManager命名空間中也建立了一個CACHER類,來緩存HandlerConfiguration配置項。之所以不把他們放在一個CATCHER類里,是為了避免裝箱操作。
優化后,我以首頁為例進行優化前后的對比,結果如下:
Application Center Test
概述: 摘要
| (1) | 測試名稱: | ACTSamples: 新BLOG |
| 測試運行名稱: | report-新BLOG-二月 25, 2006 10-51-30 | |
| 測試開始時間: | 2006-2-25 10:50:28 | |
| 測試持續時間: | 00:00:01:00 | |
| 測試迭代次數: | 3,760 | |
| 測試說明: | - |
| (2) | 測試名稱: | ACTSamples: 老Blog |
| 測試運行名稱: | report-老Blog-二月 25, 2006 10-52-40 | |
| 測試開始時間: | 2006-2-25 10:51:38 | |
| 測試持續時間: | 00:00:01:00 | |
| 測試迭代次數: | 1,661 | |
| 測試說明: | - |
測試運行圖形
顯示錯誤| 屬性 | |||||
| ? | (1) | (2) | |||
| 測試類型: | 動態 | 動態 | |||
| 瀏覽器同時連接數: | 20 | 20 | |||
| 準備時間(秒): | |||||
| 測試持續時間: | 00:00:01:00 | 00:00:01:00 | |||
| 測試迭代次數: | 3,760 | 1,661 | |||
| 生成的詳細測試結果: | 是 | 是 | |||
| ? | |||||
| 摘要 | |||||
| ? | (1) | (2) | |||
| 請求總數: | 3,762 | 1,663 | |||
| 連接總數: | 3,760 | 1,661 | |||
| ? | |||||
| 每秒平均請求數: | 62.70 | 27.72 | |||
| 首字節平均響應時間(毫秒): | 298.10 | 691.10 | |||
| 末字節平均響應時間(毫秒): | 299.60 | 693.47 | |||
| 每次迭代末字節平均響應時間(毫秒): | 299.76 | 694.30 | |||
| ? | |||||
| 測試中的唯一請求數: | 1 | 1 | |||
| 唯一響應代碼數: | 1 | 1 | |||
| ? | |||||
| 錯誤計數 | |||||
| ? | (1) | (2) | |||
| HTTP: | |||||
| DNS: | |||||
| 套接字: | |||||
| ? | |||||
| 其他網絡統計數據 | |||||
| ? | (1) | (2) | |||
| 平均帶寬(字節/秒): | 1,182,812.80 | 510,220.68 | |||
| ? | |||||
| 發送字節數(字節): | 1,437,970 | 624,866 | |||
| 接收字節數(字節): | 69,530,798 | 29,988,375 | |||
| ? | |||||
| 發送字節平均速率(字節/秒): | 23,966.17 | 10,414.43 | |||
| 接收字節平均速率(字節/秒): | 1,158,846.63 | 499,806.25 | |||
| ? | |||||
| 連接錯誤數: | |||||
| 發送錯誤數: | |||||
| 接收錯誤數: | |||||
| 超時錯誤數: | |||||
| ? | |||||
| 響應代碼 | |||||
| ? | (1) | (2) | |||
| Response Code: 200 - 請求已成功完成。 | |||||
| 計數: | 3,762 | 1,663 | |||
| 百分比(%): | 100.00 | 100.00 | |||
| ? | |||||
可以看到,效率提高了2。3倍。
以上我緩存的只是訪問比較頻繁的兩個配置項,我們可以將所有的配置都緩存。以使DOTTEXT性能達到最優。
轉載于:https://www.cnblogs.com/ddwinter/archive/2006/03/01/340434.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的优化DOTTEXT之一:缓存配置项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring + hibernate
- 下一篇: Live Messenger 邀请,再次