2012-12-21 50 views
8

我對理解HttpCache和Singleton方法有點困惑。HttpCache vs Singleton - MVC應用程序的最佳實踐

我的應用程序使用Asp.net MVC,場景是,我有一些永遠不會改變的數據列表和一些可能很少更改的數據。

我已經開發並部署了使用Singleton存儲庫的應用程序來處理這種類型的數據。 它表現很好。唯一的問題是,當發生罕見情況時,我必須重新啓動IIS才能生效。

什麼是最佳解決方案?

辛格爾頓實施

public class SingletonRepository : ISingletonRepository 
    { 
     private static SingletonRepository singleInstance; 

     private readonly IStateRepository stateRepo; 
     private readonly ICountryRepository countryRepo; 
     private readonly ITDPaymentModeRepository paymentModeRepo; 
     private readonly ITDPlanRepository planRepo; 
     private readonly ITDOrderTypeRepository orderTypeRepo; 
     private readonly IKeywordRepository keywordRepo; 
     private readonly IAgencyRepository agencyRepo; 

     private readonly IList<AT_STATE> lstState; 
     private readonly IList<AT_COUNTRY> lstCountry; 
     private readonly IList<TDPaymentMode> lstPaymentMode; 
     private readonly IList<TDPlan> lstPlan; 
     private readonly IList<TDOrderType> lstOrderType; 
     private readonly IList<Keyword> lstKeyword; 
     private readonly IList<Agency_MST> lstAgency; 

     private SingletonRepository() 
     { 
      stateRepo = new StateRepository(); 
      countryRepo = new CountryRepository(); 
      paymentModeRepo = new TDPaymentModeRepository(); 
      planRepo = new TDPlanRepository(); 
      orderTypeRepo = new TDOrderTypeRepository(); 
      keywordRepo = new KeywordRepository(); 
      agencyRepo = new AgencyRepository(); 

      lstState = stateRepo.GetAll().Where(x => x.CountryId == 101).ToList(); 
      lstCountry = countryRepo.GetAll().ToList(); 
      lstPaymentMode = paymentModeRepo.GetAll().ToList(); 
      lstPlan = planRepo.GetAll().ToList(); 
      lstOrderType = orderTypeRepo.GetAll().ToList(); 
      lstKeyword = keywordRepo.GetAll().ToList(); 
      lstAgency = agencyRepo.GetAll().ToList(); 

      //lstState = stateRepo.GetAll().Take(20).ToList(); 
      //lstCountry = countryRepo.GetAll().Take(20).ToList(); 
      //lstPaymentMode = paymentModeRepo.GetAll().Take(20).ToList(); 
      //lstPlan = planRepo.GetAll().Take(20).ToList(); 
      //lstOrderType = orderTypeRepo.GetAll().Take(20).ToList(); 
      //lstKeyword = keywordRepo.GetAll().Take(20).ToList(); 
      //lstAgency = agencyRepo.GetAll().Take(20).ToList(); 
     } 

     public static SingletonRepository Instance() 
     { 
      return singleInstance ?? (singleInstance = new SingletonRepository()); 
     } 

     public IList<AT_STATE> GetState() 
     { 
      return this.lstState; 
     } 
     public IList<AT_COUNTRY> GetCountry() 
     { 
      return this.lstCountry; 
     } 

     public IList<TDPaymentMode> GetPaymentMode() 
     { 
      return this.lstPaymentMode; 
     } 

     public IList<TDPlan> GetPlan() 
     { 
      return this.lstPlan; 
     } 

     public IList<TDOrderType> GetOrderType() 
     { 
      return this.lstOrderType; 
     } 

     public IList<Keyword> GetKeyword() 
     { 
      return this.lstKeyword; 
     } 

     public IList<Agency_MST> GetAgency() 
     { 
      return this.lstAgency; 
     }  

    } 

}

+0

你的單身人士如何實施?它只是將數據存儲在內存中(作爲類成員)?在很少更改的數據更改之後,您是否必須重新啓動IIS以便將其顯示給用戶? – danludwig

+0

是的,數據存儲在IList 對象中。並且只有在IIS重新啓動後纔會反映更改。 –

回答

8

使用單例模式的目的通常是不適合的靜態數據的存儲。如果只希望一個對象實例能夠執行某些操作,則應該使用單例。它可能很快,但正如您所看到的,當數據發生變化時,您需要重置堆以獲取新數據(正如您所說,通過重新啓動IIS)。

HttpCache(更具體地說,Http緩存默認使用的ObjectCache)將數據存儲在與堆相同的位置:在隨機存取存儲器中。因此,它與存儲在堆上的類或實例中的靜態數據一樣快。區別在於您可以將緩存設置爲定期過期,以便在數據更改時獲取新數據。你甚至可以設置SqlCacheDependencies,這樣只要數據庫的狀態發生變化,緩存就會失效。

緩存的另一個優點是它可以更有效地利用服務器的RAM資源。無論如何,即使在數據未被使用的情況下,這些數據也會佔用內存。使用緩存時,服務器只在使用時將數據存儲在內存中。緩存的缺點在於,偶爾會有用戶在緩存過期後請求數據時,這裏和那裏的響應速度會變慢。但是,在他們這樣做之後,其他用戶將從緩存的數據中獲益一段時間。

+1

我剛剛使用了Singleton,因爲我不希望有任何不希望發生的性能消耗。所以簡單地把所有的List對象放在內存中。但我認爲使用只讀列表,我可以使用混合方法通過使用cachedependancy來通知我的數據更改和編寫句柄來修改我的列表數據..請更正。 –

+0

答案就是我正在尋找的。 –

3

實際上,你可以通過創建一個重載的方法,如本重裝單:

public static void ReloadSingletonRepositoryInstance() 
{ 
    singleInstance = null; 
    SingletonRepository sr = SingletonRepository.Instance; 
} 

希望有所幫助。