Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpCache vs Singleton - Best practice for an MVC application

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

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.

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.?

Singleton implementation

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

    }

}

like image 525
Dipal Mehta Avatar asked Dec 21 '12 12:12

Dipal Mehta


1 Answers

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 (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.

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.

like image 89
danludwig Avatar answered Sep 28 '22 06:09

danludwig