HttpCache vs Singleton - MVC应用程序的最佳实践

时间:2012-12-21 12:55:22

标签: asp.net-mvc singleton http-caching

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

我的应用程序使用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;
        }      

    }

}

2 个答案:

答案 0 :(得分:8)

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

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

缓存的另一个优点是它可以更有效地利用服务器的RAM资源。使用您的单例,无论如何,即使数据未被使用,这些数据也将始终占用内存。使用缓存时,服务器仅在使用时将数据存储在内存中。缓存的缺点是偶尔,当缓存过期后,用户在这里和那里请求数据时会得到较慢的响应。但是,在他们这样做之后,其他用户将受益于缓存一段时间的数据。

答案 1 :(得分:3)

您实际上可以通过创建重载方法重新加载单例:

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

希望有所帮助。