HttpCache VS辛格尔顿 - 一个MVC应用程序的最佳实践 [英] HttpCache vs Singleton - Best practice for an MVC application

查看:90
本文介绍了HttpCache VS辛格尔顿 - 一个MVC应用程序的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑在认识HttpCache和辛格尔顿的做法。

I am little bit confused in understanding of the HttpCache and Singleton approaches.

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

My application uses Asp.net MVC and the scenario is that, I have some List of Data that will never change and some data that might rarely change.

我已经开发并使用这种类型的数据存储库辛格尔顿部署的应用程序。
它执行很大。唯一的问题是,一个罕见的情况下发生的时候,我必须重新启动IIS即可生效。

I have developed and deployed application using a Singleton repository for this type of data. It performs great. The only issue is that when a rare case occur, i have to restart IIS to take effect.

什么是最好的解决方案?

What is the best solution.?

辛格尔顿执行

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模式的目的通常不是静态的数据存储。你应该使用一个单身的时候,你只需要一个对象实例,以便能够执行某些操作。这可能是快,但你可以看到,当数据发生变化时,需要重新设置堆来获得新的数据(如你所说,重新启动IIS)。

The purpose of using the singleton pattern generally is not for static data storage. You should use a singleton when you only want one object instance to be able to perform certain actions. It may be fast, but as you can see, when the data changes, you need to reset the heap to get the new data (as you say, by restarting IIS).

HttpCache(更确切地说,它HTTP缓存默认使用ObjectCache),数据存储在同一个地方堆:在随机存取存储器。因此,它是存储在堆中一类或实例一样快,静态数据。不同的是,你可以设置高速缓存定期去​​陈旧,因此它会得到数据改变时新数据。你甚至可以设置SqlCacheDependencies因此缓存是由陈旧的,只要你的数据库的状态变化。

HttpCache (more specifically, the ObjectCache which Http caching uses by default), stores the data in the same place as the heap: in Random Access Memory. So, it is just as fast as static data stored in a class or instance on the heap. The difference is that you can set up the cache to periodically go stale, so that it will get new data when the data changes. You can even set up SqlCacheDependencies so that the cache is made stale whenever your database's state changes.

缓存的另一个优点是,它更有效地利用服务器的RAM资源。随着你的单,不管是什么,这个数据会一直占用内存,即使不被使用的数据。与高速缓存中,在使用时它的服务器只存储在存储器中的数据。与高速缓存中的缺点是偶尔,在这里,用户有会得到更慢的响应时,请求数据的高速缓存已经过期之后。然而,在他们这样做,其他用户将受益于被缓存一段时间的数据。

Another advantage of cache is that it more efficiently utilizes your server's RAM resources. With your singleton, no matter what, this data will always be occupying memory, even when the data is not being used. With cache, the server only stores the data in memory when it is being used. The disadvantages with cache are that occasionally, a user here and there will get a slower response when they request data after the cache has expired. However, after they do, other users will benefit from the data being cached for a time.

这篇关于HttpCache VS辛格尔顿 - 一个MVC应用程序的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆