What algorithm is .NET using to calculate cache expiration time

0 votes
asked Aug 27, 2010 by rizzle

I'm running this code which appends a timestamp to a file every time the cache expires, but the timestamps don't seem to match up with the absolute expiration tht i've applied

public void ProcessRequest(HttpContext context)
{
    System.IO.File.AppendAllText("c:/test.txt", "new");

    context.Cache.Add("test", DateTime.Now.ToString(), null, DateTime.Now.AddSeconds(25), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, new System.Web.Caching.CacheItemRemovedCallback(callback));
        }

public static void callback(string str, object obj, System.Web.Caching.CacheItemRemovedReason reason)
        {
            System.IO.File.AppendAllText("c:/test.txt", " \n at " + DateTime.Now.ToString() + " this expired: " + obj.ToString());

            HttpRuntime.Cache.Add("test", DateTime.Now.ToString(), null, DateTime.Now.AddSeconds(25), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, new System.Web.Caching.CacheItemRemovedCallback(callback));
        }

code says it should expire every 25 seconds, but the logs are consistently at 40 seconds. I've tried 5 seconds expiration and 10 second expiration and the logs were at 20 second intervals.

new 
 at 8/27/2010 1:16:40 PM this expired: 8/27/2010 1:16:01 PM 
 at 8/27/2010 1:17:20 PM this expired: 8/27/2010 1:16:40 PM 
 at 8/27/2010 1:18:00 PM this expired: 8/27/2010 1:17:20 PM 
 at 8/27/2010 1:18:40 PM this expired: 8/27/2010 1:18:00 PM 
 at 8/27/2010 1:19:20 PM this expired: 8/27/2010 1:18:40 PM 
 at 8/27/2010 1:20:00 PM this expired: 8/27/2010 1:19:20 PM 
 at 8/27/2010 1:20:40 PM this expired: 8/27/2010 1:20:00 PM 
 at 8/27/2010 1:21:20 PM this expired: 8/27/2010 1:20:40 PM 
 at 8/27/2010 1:22:00 PM this expired: 8/27/2010 1:21:20 PM 
 at 8/27/2010 1:22:40 PM this expired: 8/27/2010 1:22:00 PM 
 at 8/27/2010 1:23:20 PM this expired: 8/27/2010 1:22:40 PM 

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...