微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我对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;
        }      

    }

}

解决方法

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

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

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

原文地址:https://www.jb51.cc/aspnet/248928.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐