When will this cache object expire?

If you don't put any expiration policy on the Cache object than it can only under certain scenarious. Take a look at the code below where I did not establish any expiration policy for the cache object.

  private DataSet GetDataSetNoDatabaseDependency()
    {
        DataSet ds = 
null;

        
if (Cache["MyDataSet"] != null)
        {
            
// This means that Cache object contains Data 
            
ds = (DataSet)Cache["MyDataSet"];
        }
        
else
        
{
            
// Adds the dependency to the Cache object 

            
SqlConnection myConnection = new SqlConnection(GetConnectionString());
            SqlDataAdapter ad = 
new SqlDataAdapter("SELECT * FROM Categories", myConnection);
            ds = 
new DataSet();
            ad.Fill(ds, "Categories");           
            
            Cache.Insert("MyDataSet", ds);          
           
            
          
             
        }
        
return ds; 

    }  

In this case you will always get the data that was inserted in the cache. This means that you will always get the stale data.

In this case cache will expire but the conditions are: If you build the assembly again and hence all sessions will be lost OR if you fill the cache object and it needs space than it will remove the least recently used items from the memory.

The bottom line is that whenever you insert something in the cache object always set the expiration time or else it will remain in the cache object for a long time.

 

 

powered by IMHO

Print | posted @ Sunday, September 18, 2005 8:08 PM

Twitter