gpt4 book ai didi

asp.net-mvc - HttpCache vs Singleton - MVC 应用程序的最佳实践

转载 作者:行者123 更新时间:2023-12-04 00:26:47 25 4
gpt4 key购买 nike

我对 HttpCache 和 Singleton 方法的理解有点困惑。

我的应用程序使用 Asp.net MVC,场景是,我有一些永远不会改变的数据列表和一些可能很少改变的数据。

我已经使用单例存储库为此类数据开发和部署了应用程序。
它表现出色。唯一的问题是,当发生罕见的情况时,我必须重新启动 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;
}

}

}

最佳答案

使用单例模式的目的一般不是为了静态数据存储。当您只希望一个对象实例能够执行某些操作时,您应该使用单例。它可能很快,但是正如您所看到的,当数据更改时,您需要重置堆以获取新数据(如您所说,通过重新启动 IIS)。

HttpCache(更具体地说,Http 缓存默认使用的 ObjectCache)将数据存储在与堆相同的位置:随机存取存储器中。因此,它与存储在堆上的类或实例中的静态数据一样快。不同之处在于您可以将缓存设置为定期失效,以便在数据更改时获取新数据。您甚至可以设置 SqlCacheDependencies,以便每当您的数据库状态发生变化时,缓存就会失效。

缓存的另一个优点是它可以更有效地利用服务器的 RAM 资源。对于你的单例,无论如何,这些数据总是会占用内存,即使数据没有被使用。使用缓存,服务器仅在使用时将数据存储在内存中。缓存的缺点是,有时用户在缓存过期后请求数据时会得到较慢的响应。但是,在他们这样做之后,其他用户将受益于暂时缓存的数据。

关于asp.net-mvc - HttpCache vs Singleton - MVC 应用程序的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13990623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com