jMiguel LA
jMiguel LA

Reputation: 350

How to have permanent cached data in IIS6

I have an application in C# (FrameWork 4.0) running on IIS6.

I'd like to have cached data data (class at foot) permanently, but an event or something similar removes this cached data, in a short time (15-25 mins).

I configure the IIS only with this option: Recycle worker processes (in minutes) 1740.

Any ideas to preserve the data cached? Is the filetime of a static variable is the same as the application lifetime?

public class CacheUsers
{
        static object objlock;
        private static List<Users> usersList { get; set; }
        private static DateTime lastUpdate;

        public CacheUsers()
        {
            if (objlock == null)
            {
                objlock = new object();
            }
            lock (objlock)
            {
                if (usersList == null || usersList.Count.Equals(0) || (lastUpdate < DateTime.Now.AddHours(-24)))
                {
                    UpdateUserList();
                }
            }
        }

        public static List<Users> ListaCacheada
        {
            get
            {
                if (usersList == null || usersList.Count.Equals(0) || (lastUpdate < DateTime.Now.AddHours(-24)))
                {
                    UpdateUserList();
                }
                return usersList;
            }
        }

        /// <summary>
        /// Refresh the user list
        /// </summary>
        private static void UpdateUserList()
        {
            usersList = GetUsers();
            lastUpdate = DateTime.Now;
        }

}

Upvotes: 0

Views: 1336

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100620

Edit: How long objects can survive -

There is technically no way for CLR objects in an AppDomain can survive restart of that AppDomain (i.e. in ASP.Net it can caused by change in web.config), as result no way of surviving AppPool recycle (process shutdown). Native objects will not be able to survive AppPool recycle either.

Recycle of an AppPool not necessary will create process again (it actually will not), only first request to a site served by this AppPool will launch process.

So if your goal is strictly "my CLR objects must be in some sort of cache immediately after AppPool recycle" the closest would be to re-cache objects in your Application_Start (or equivalent method in a module). Note that static objects are not guaranteed to be created at assembly load time, so if you want to use static objects for caching you still need to trigger the creation i.e. by accessing them in Application_Start.

If you need objects to live beyond the AppPool's lifetime - you need a separate service. Such systems have to keep objects outside of the process (and sometimes the machine), so you'll pay the cost of remotely accessing such objects.

You may be interested in distributed caching (like memcachd) to see if it fits your needs.


Normal "caching" usage is to cache objects that are costly to create, it is unclear want to achieve with your "cached" definition.

It is often done by hiding caching logic inside method that provides the object:

MyObject GetMyObject()
{
  MyObject result= cacheProvider.Get("cacheId_MyObject");
  if (result = null)
  { 
    result = VerySlowMethodToCreateMyObject();
    cacheProvider.Cache("cacheId_MyObject", result);
  }
  return result;
}

Consider simply using System.Web.Caching.Cache. Its Insert method provides a lot of options. You can even implement "cached forever" items if you provide onRemoveCallback.

Upvotes: 1

jml
jml

Reputation: 1

I've chaged the code, and I use HttpRuntime.Cache, and nothing happens. :-(

The problem is that App Pool should only works only once per day, because:

I configure the IIS only with this option: Recycle worker processes (in minutes) 1740

and seems that something is launching this process a lot of times (aprox, every 15 minutes).

I just want to stop the App Pool recycler. Who can I do it?

    public class CacheUsers  
    {          
         static object objlock;          
        private static List<Users> usersList { get; set; }          


    public CacheUsers()          
    {              
        if (objlock == null)              
        {                  
            objlock = new object();              
        }              
        lock (objlock)              
        {                  
            if (usersList == null || usersList.Count.Equals(0))                  
            {                      
                UpdateUserList();                  
            }              
        }          
    } 

    private static void UpdateUserList()          
    {         
                 usersList = GetList(); 
                HttpRuntime.Cache.Insert("Key", usersList , null,
                            Cache.NoAbsoluteExpiration,
                            Cache.NoSlidingExpiration,
                            System.Web.Caching.CacheItemPriority.NotRemovable,
                            ReportRemovedCallback
                            );               
    }    




}  

ReportRemovedCallback indicates the cause of this: Removed. But, who does it?

Upvotes: 0

Related Questions