WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
SPB-設(shè)計(jì)機(jī)制-緩存機(jī)制
導(dǎo)航: 上一頁(yè)
緩存用途
緩存是SPB中非常重要的一個(gè)臨時(shí)存儲(chǔ),是提升性能的一個(gè)非常重要的設(shè)計(jì)機(jī)制。緩存的作用主要有如下幾點(diǎn):
- 緩存從數(shù)據(jù)庫(kù)查詢到的數(shù)據(jù),減少與數(shù)據(jù)庫(kù)的連接次數(shù);
- 緩存從配置文件讀取的數(shù)據(jù),==減少==對(duì)文件系統(tǒng)的讀寫次數(shù);
- 緩存依靠反射進(jìn)行實(shí)例化的類的實(shí)例,減少反射的次數(shù);
- 緩存View在某個(gè)皮膚中對(duì)應(yīng)的aspx/ascx路徑,減少View文件查找的時(shí)間;
緩存數(shù)據(jù)庫(kù)中的數(shù)據(jù)是緩存在SpaceBuilder中最重要的用途?;旧纤袛?shù)據(jù)都是在業(yè)務(wù)邏輯層首先在緩存中獲取,如果找不到則從數(shù)據(jù)庫(kù)中獲取并緩存在內(nèi)存中供下一次訪問使用。SPB在提升性能的同時(shí)也必須考慮緩存與數(shù)據(jù)庫(kù)實(shí)際數(shù)據(jù)的同步性,因此需要在增、刪、改時(shí)手動(dòng)使相應(yīng)的緩存失效。
使用規(guī)則:
緩存使用示例代碼:(獲取SPBCacheManager)
SPBCacheFactory.GetCacheManager(string cacheManagerName);
如何使用緩存
- 配置CacheManage
在web.config的<cacheManagers>節(jié)點(diǎn)配置cacheManager
<cacheManagers> <add expirationPollFrequencyInSeconds="120" maximumElementsInCacheBeforeScavenging="1000000" numberToRemoveWhenScavenging="10" backingStoreName="Null Storage" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching" name="Blog" /> </cacheManagers>
- 獲取SPBCacheManager
SPBCacheManager CacheManager = SPBCacheManagerFactory.GetCacheManager(“Blog”);
- 緩存API
過期策略
- 絕對(duì)時(shí)間
CacheManager.Add(cacheKey, pds, SPBCacheManager.CachingExpirationType.ObjectCollection);
預(yù)定義的SPBCacheManager.CachingExpirationType包含以下幾種類型:
- 文件依賴
一般對(duì)于配置文件的緩存采用文件依賴,即當(dāng)配置文件發(fā)生變化后,緩存自動(dòng)失效。例如:
string path = HttpContext.Current.Server.MapPath("~/SpaceBuilder.config"); config = new SPBConfig(path); cacheManager.Add(cacheKey, config, path);
- 手動(dòng)過期
緩存對(duì)于性能的提升有諸多貢獻(xiàn),但是也存在一個(gè)常見的問題即緩存的數(shù)據(jù)與實(shí)際數(shù)據(jù)庫(kù)的數(shù)據(jù)不同步,存在一定時(shí)間的延遲。為了解決這個(gè)問題,SpaceBuilder采用了緩存的手動(dòng)過期策略。例如,以下代碼用于獲取ForumThread,并把數(shù)據(jù)放入緩存:
public static ForumThread GetThread(int threadID, bool useCache) { string cacheKey = string.Format("ForumThread:{0}", threadID); ForumThread thread = null; if (useCache) { thread = CacheManager.Get(cacheKey) as ForumThread; } if (thread == null) { thread = ForumDataProvider.Instance().GetThread(threadID); if (useCache) { CacheManager.Add(cacheKey, thread, SPBCacheManager.CachingExpirationType.SingleObject); } } return thread; }
對(duì)于該緩存,只有兩種情況需要使其失效:該BlogThread被修改及被刪除,因此我們應(yīng)該在以下代碼中手動(dòng)使其失效:
public static void UpdateThread(ForumThread thread) { #region 設(shè)置AuditingStatus if (Auditings.NeedAuditing(thread.UserID, AuditingItemKeys.Instance().ForumThread(), AuditingStrictDegrees.Update)) thread.IsApproved = false; #endregion thread.Subject = WebUtils.HtmlEncode(thread.Subject); ForumEvents.PreBeforeForumThreadChange(thread, ObjectState.Update); ForumEvents.BeforeForumThreadChange(thread, ObjectState.Update); ForumDataProvider.Instance().CreateUpadateThread(thread, DataProviderAction.Update); ForumEvents.AfterForumThreadChange(thread, ObjectState.Update); //清除緩存 ClearCache(thread.SectionID, thread.ThreadID); } public static void DeleteThread(int threadID) { ForumThread thread = ForumThreads.GetThread(threadID); if (thread != null) { #region 刪除附件 ForumPostQuery query = new ForumPostQuery(); query.EnablePaging = false; query.PageSize = ValueHelper.GetSafeSqlInt(int.MaxValue); query.IsApproved = null; query.ThreadID = thread.ThreadID; //刪除回復(fù)附件 PagingDataSet<ForumPost> ps = ForumPosts.GetPosts(query); foreach (ForumPost fp in ps.Records) { ForumAttachmentManager.Instance().DeleteAttachmentsByAssociateID(fp.PostID); } //刪除主題附件 ForumAttachmentManager.Instance().DeleteAttachmentsByAssociateID(thread.PostID); #endregion ForumEvents.PreBeforeForumThreadChange(thread, ObjectState.Delete); ForumEvents.BeforeForumThreadChange(thread, ObjectState.Delete); ForumDataProvider.Instance().DeletePost(thread.PostID); ForumEvents.AfterForumThreadChange(thread, ObjectState.Delete); //清除緩存 ClearCache(thread.SectionID, thread.ThreadID); } } /// <summary> /// 清除相關(guān)緩存 ///</summary> private static void ClearCache(int sectionID, int threadID) { if (threadID > 0) { CacheManager.Remove(string.Format("ForumThread:{0}", threadID)); CacheManager.RemoveItemsByStartsWith(string.Format("ForumPostSet::ThreadID:{0}", threadID)); } if (sectionID > 0) { CacheManager.RemoveItemsByStartsWith(string.Format("ForumThreads::SectionID:{0}", sectionID)); CacheManager.RemoveItemsByStartsWith(string.Format("ForumThreads::SectionID:{0}", -1)); } else { CacheManager.RemoveItemsByStartsWith("ForumThreads::"); } }
所以為了實(shí)施緩存的手動(dòng)過期策略,一定要全面分析會(huì)影響某一緩存的所有操作,然后在這些操作中清除相應(yīng)的緩存。
注意事項(xiàng)
- 每個(gè)應(yīng)用建立一個(gè)自己的緩存區(qū)間(CacheManager),在web.config中進(jìn)行配置;
- CacheKey大小寫不敏感;
- CacheKey一定要保持唯一,需要制定CacheKey的命名規(guī)則;