asp.net-mvc – HttpCache vs Singleton – MVC应用程序的最佳实践
|
我对HttpCache和Singleton方法的理解有点困惑. 我的应用程序使用Asp.net MVC,方案是,我有一些永远不会改变的数据列表和一些可能很少改变的数据. 我已经使用Singleton存储库为这种类型的数据开发和部署了应用程序. 什么是最好的解决方案. 单身实施 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;
}
}
} 解决方法使用单例模式的目的通常不是静态数据存储.当您只希望一个对象实例能够执行某些操作时,您应该使用单例.它可能很快,但正如您所看到的,当数据发生变化时,您需要重置堆以获取新数据(如您所说,通过重新启动IIS).HttpCache(更具体地说,Http缓存默认使用的ObjectCache)将数据存储在与堆相同的位置:在随机存取存储器中.因此,它与存储在堆上的类或实例中的静态数据一样快.不同之处在于您可以将缓存设置为定期过时,以便在数据更改时获取新数据.您甚至可以设置SqlCacheDependencies,以便在数据库状态发生更改时使缓存失效. 缓存的另一个优点是它可以更有效地利用服务器的RAM资源.使用您的单例,无论如何,即使数据未被使用,这些数据也将始终占用内存.使用缓存时,服务器仅在使用时将数据存储在内存中.缓存的缺点是偶尔,用户在缓存过期后请求数据时会得到较慢的响应.但是,在他们这样做之后,其他用户将受益于缓存一段时间的数据. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 在Google Chrome浏览器中加载两次
- 为什么我的内联asp.net无法正常工作?
- asp.net – 具有管道模式=集成的IIS 7.0不会在ASP .NET中加
- asp.net-mvc – 什么是防伪令牌盐的使用?
- asp.net-mvc-3 – 根据asp.net mvc3中的下拉列表数据获取列
- asp-classic – %%和经典asp之间有什么区别?
- asp.net-web-api – 随着SerilogWeb.Owin停产,是否有“官方
- asp.net-mvc – 在MVC Razor中,如何在子布局下面定义一个Re
- 哪个.NET框架与MVC 4?
- entity-framework – ASP.NET-Identity限制UserName长度
- 如何在ASP.NET 5中添加一个TypeScript绝对类型的
- asp.net – 有没有办法以编程方式设置Elmah中的应
- ASP.NET MVC:如何在MVC应用程序中使用静态HTML页
- Razor页面在运行时无法在ASP.NET Core RC2中看到
- asp.net – 在IIS 7或IIS 7.5集成模式下,默认文档
- 如何在ASP.NET全球化中配置不变文化?
- iis-7 – ASP.NET MVC4在IIS7集成模式下不处理PO
- asp.net – 如何实时压缩和流量到Response.Outpu
- asp.net – 诊断“请求超时”HttpExceptions
- 动态渲染asp:从ASP.NET中的BLOB条目的图像
