Asp.Net Catch的應用

來源:互聯網
上載者:User
雖然現在已經是寬頻時代,小貓已經離我們漸漸遠去,可作為WEB應用開發人員來說,我們仍然有責任和義務不斷地通過技術手段來最佳化WEB應用效能,讓使用者瀏覽時少一些等待,多一些爽快。
       所幸的是,ASP.NET作為基於.Net Framework的WEB開發技術,它也享用著.Net Framework的優越性,.Net Framework為我們提供了良好的Cache技術,使我們能開發出速度更快、使用者體驗更好的WEB應用。命名空間System.Web.Caching提供了Cache類,其Cache的有效性依賴分以下三種情況:
1. 時間點(指定時間點內有效);
2. KEY值(KEY值作Cache項標識);
3. 檔案或目錄(指定檔案或目錄變更,則原Cache項不可用);
       下面我就結合實際開發的應用跟大家分享一下使用Cache來提高ASP.NET應用的效能。
       我們在開發中常常會遇到讀取記錄列表(例如最新動向的新聞列表Top N)、記錄本身(例如一條新聞),使用者訪問的時候,這樣的資訊是否每次都要重複從資料庫中讀取呢?聰明的你可能知道,這完全是沒必要的。

我們為了方便處理,不防設計一個SiteCache類(借鑒了CS中的CSCache.cs),並提供若干靜態方法,來負責處理Cache項的添加和刪除。

代碼:
SiteCache.cs
  1using System;
  2using System.Collections;
  3using System.Text.RegularExpressions;
  4using System.Web;
  5using System.Web.Caching;
  6
  7namespace Ycweb.Components
  8{
  9    public class SiteCache
 10    {
 11        private static readonly Cache _cache;
 12        public static readonly int DayFactor;
 13        private static int Factor;
 14        public static readonly int HourFactor;
 15        public static readonly int MinuteFactor;
 16
 17        static SiteCache()
 18        {
 19            DayFactor = 17280;
 20            HourFactor = 720;
 21            MinuteFactor = 12;
 22            Factor = 5;
 23            _cache = HttpRuntime.Cache;
 24        }
 25
 26        private SiteCache()
 27        {
 28        }
 29
 30        public static void Clear()
 31        {
 32            IDictionaryEnumerator enumerator = _cache.GetEnumerator();
 33            while (enumerator.MoveNext())
 34            {
 35                _cache.Remove(enumerator.Key.ToString());
 36            }
 37        }
 38
 39        public static object Get(string key)
 40        {
 41            return _cache[key];
 42        }
 43
 44        public static void Insert(string key, object obj)
 45        {
 46            Insert(key, obj, null, 1);
 47        }
 48
 49        public static void Insert(string key, object obj, int seconds)
 50        {
 51            Insert(key, obj, null, seconds);
 52        }
 53
 54        public static void Insert(string key, object obj, CacheDependency dep)
 55        {
 56            Insert(key, obj, dep, HourFactor*12);
 57        }
 58
 59        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
 60        {
 61            Insert(key, obj, null, seconds, priority);
 62        }
 63
 64        public static void Insert(string key, object obj, CacheDependency dep, int seconds)
 65        {
 66            Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
 67        }
 68
 69        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
 70        {
 71            if (obj != null)
 72            {
 73                _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double) (Factor*seconds)), TimeSpan.Zero, priority, null);
 74            }
 75        }
 76
 77        public static void Max(string key, object obj)
 78        {
 79            Max(key, obj, null);
 80        }
 81
 82        public static void Max(string key, object obj, CacheDependency dep)
 83        {
 84            if (obj != null)
 85            {
 86                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
 87            }
 88        }
 89
 90        public static void MicroInsert(string key, object obj, int secondFactor)
 91        {
 92            if (obj != null)
 93            {
 94                _cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double) (Factor*secondFactor)), TimeSpan.Zero);
 95            }
 96        }
 97
 98        public static void Remove(string key)
 99        {
100            _cache.Remove(key);
101        }
102
103        public static void RemoveByPattern(string pattern)
104        {
105            IDictionaryEnumerator enumerator = _cache.GetEnumerator();
106            Regex regex1 = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
107            while (enumerator.MoveNext())
108            {
109                if (regex1.IsMatch(enumerator.Key.ToString()))
110                {
111                    _cache.Remove(enumerator.Key.ToString());
112                }
113            }
114        }
115
116        public static void ReSetFactor(int cacheFactor)
117        {
118            Factor = cacheFactor;
119        }
120
121
122
123    }
124}

       其實該類主要就是利用前文所提及的關於Cache依賴項的第一點與第二點的特性來維護我們自己的Cache項。
有了SiteCache類,接下來看看如何使用它。還是以讀取新聞TonN列表為例:

 1public static RecordSet GetNewsSetTopN(string classCode,int topN,SortPostsBy orderBy, SortOrder sortOrder, string language)
 2{
 3 string cacheKey = string.Format("NewsSetTopN-LG:{0}:CC:{1}:TN:{2}:OB:{3}:SO:{4}", language,classCode,topN.ToString(), orderBy.ToString(),sortOrder.ToString());
 4 
 5 //從上下文中讀快取項目
 6 RecordSet newsSet = HttpContext.Current.Items[cacheKey] as RecordSet;
 7 if (newsSet == null)
 8 {
 9  //從HttpRuntime.Cache讀快取項目
10  newsSet = SiteCache.Get(cacheKey) as RecordSet;
11  if (newsSet == null)
12  {
13   //直接從資料庫從讀取
14   CommonDataProvider dp=CommonDataProvider.Instance();
15   newsSet =dp.GetNewsSetTopN(language,classCode,topN,orderBy,sortOrder);
16   //並將結果緩衝到HttpRuntime.Cache中
17   SiteCache.Insert(cacheKey, newsSet, 60, CacheItemPriority.Normal);
18  }
19       
20 }
21return newsSet;
22}
 

       這樣在5分鐘內就不用重複訪問資料庫了來讀該列表了,當然,也有人會問,如果在這5分鐘內某條新聞刪除了或修改了怎麼辦,沒關係,我們在刪除或修改時可以根據Cache KEY來強制移除該Cache項,當然,如果你覺得你對列表的時效性不是特別在意,你可以不強制移除該Cache項,讓Cache項定義的時間點自動失效。當然,最好還是提供一個方法按匹配模式項來強行刪除Cache項就可以了,例如:

 

1/**//// <summary>
2/// 刪除匹配的NewsSetTopN列表的Cache項
3/// </summary>
4public static void ClearNewsSetTopNCache(string language,string classCode,int topN)
5{
6 string cacheKey = string.Format("NewsSetTopN-LG:{0}:CC:{1}:TN:{2}",language,classCode,topN.ToString());
7 SiteCache.RemoveByPattern(cacheKey);
8}
9

       發布新聞後調用靜態方法ClearNewsSetTopNCache()強行清除原來的TopN快取項目,例如:

 

 1/**//// <summary>
 2/// 發布(建立)新聞
 3/// </summary>
 4/// <param name="post">新聞執行個體</param>
 5/// <returns>返回狀態</returns>
 6public static int Create(News post)
 7{
 8 int status;
 9 CommonDataProvider dp=CommonDataProvider.Instance();
10 dp.CreateUpdateDeleteNews(post, DataAction.Create, out status);
11 //強制清除匹配的快取項目
12 ClearNewsSetTopNCache (post.Language, post.ClassCode,Globals.GetSiteSetting.NewsListTopN);
13 return status;
14}

That's all.若有不妥之處還望各位同行指正。

http://www.cnblogs.com/aspsir/archive/2006/07/27/461229.html

聯繫我們

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