locked
Cache disappears in spite of a high time out RRS feed

  • Question

  • User1011321991 posted

    I am creating this cache which is set to expire at 6pm . The cache is set in the manner shown below.

     

    Cache.Add("myKey", 55, null, DateTime.Today.AddDays(0).AddHours(18), Cache.NoSlidingExpiration, CacheItemPriority.High, null)

     

    So once it is created it is supposed to last till 6pm. But somehow it expires after a couple of hours of inactivity. Does the IIS remove cache after a certain time period? If yes, how do I avoid that?

     

    Any help is appreciated :)

    Wednesday, May 25, 2011 7:08 PM

Answers

  • User1468581411 posted

    Your app by default uses the default app pool. The app pool kills the worker process after 20 minutes of inactivity. It also recycles the worker process after 1740 minutes. That would also explain why your application variables keep disappearing.

     

    So what you need to do is create a new app pool. Remove the check boxes that say recylce worker process and shut down worker process. Use this app pool for your website. 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 26, 2011 11:29 AM

All replies

  • User1011321991 posted

    Another thing I noticed while trying to figure the problem, is that the application variables also disappear after a certain period of time. In spite of there being no application restart or redeploy in this timeframe.

    Wednesday, May 25, 2011 7:51 PM
  • User-481631678 posted

    IIS Removes items from cache if there is not sufficent memory on server. Also see your application pool settings in IIS. They can also cause IIS restarts which could delete cache items.

    Thursday, May 26, 2011 1:40 AM
  • User-1856974186 posted

    Yes, the cache is not guaranteed; ASP.NET can remove items from the cache any time it likes if it needs the memory. Your code should always follow a pattern that checks for the item in the cache and uses it it's there, or if not, fetch it and re-cache it.

    If this is a persistent problem, look at the memory usage on the machine and use the performance monitor to see how much memory is being used by iIS, the cache and what cache hit ratios are.

    Thursday, May 26, 2011 5:58 AM
  • User-481631678 posted

    Generally I use this type of code which checks that if item is available in cache then return that item from cache, otherwise load that item from db, save it in cache and then return it from cache. Here is a sample code snippet:-

     

            public LookupEntity[] GetValue()
            {
                string cacheKey = "SomeKey";
                //Check that record Exist In Cache or not
                if (HttpRuntime.Cache[cacheKey] == null)
                {
                    //If not then load Record from Database or any other location
                    var records = LookupEntity.GetRecord();
    
                    HttpRuntime.Cache.Insert(cacheKey, 
                        records,//Records
                        null,//No Dependency
                        Cache.NoAbsoluteExpiration,//No Absolute Expiration
                        TimeSpan.FromMinutes(30));//Expire in 30 mins
                }
                return HttpRuntime.Cache[cacheKey] as LookupEntity[];
            }

    Thursday, May 26, 2011 9:18 AM
  • User1468581411 posted

    Your app by default uses the default app pool. The app pool kills the worker process after 20 minutes of inactivity. It also recycles the worker process after 1740 minutes. That would also explain why your application variables keep disappearing.

     

    So what you need to do is create a new app pool. Remove the check boxes that say recylce worker process and shut down worker process. Use this app pool for your website. 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 26, 2011 11:29 AM