Tôi hơi bối rối khi hiểu về phương pháp tiếp cận HttpCache và Singleton.HttpCache vs Singleton - Thực tiễn tốt nhất cho ứng dụng MVC
Ứng dụng của tôi sử dụng Asp.net MVC và kịch bản là, tôi có một số Danh sách dữ liệu sẽ không bao giờ thay đổi và một số dữ liệu hiếm khi có thể thay đổi.
Tôi đã phát triển và triển khai ứng dụng bằng cách sử dụng kho lưu trữ Singleton cho loại dữ liệu này. Nó hoạt động rất tốt. Vấn đề duy nhất là khi một trường hợp hiếm xảy ra, tôi phải khởi động lại IIS để có hiệu lực.
Giải pháp tốt nhất là gì?
Singleton thực hiện
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;
}
}
}
Singleton của bạn được triển khai như thế nào? Liệu nó chỉ lưu trữ dữ liệu trong bộ nhớ (là thành viên lớp)? Bạn có phải khởi động lại IIS sau khi thay đổi dữ liệu hiếm khi thay đổi để làm cho nó hiển thị cho người dùng không? – danludwig
Có, Dữ liệu được lưu trữ trong đối tượng IList. và chỉ sau khi IIS khởi động lại các thay đổi được phản ánh. –