0

I have the code like below. When I open the page for the first time I get the data from the query, subsequent times I open the page I get data from the cache and query didn't run. But when I open the page some time later, like 2 hours later, I don't get data from cache but instead the query runs again... If the absolute Expiration is set to more 26 hours, why cache is not available anymore after 2 hours? What am I missing here? There are some global parameter to configure maximum cache time at IIS or similar?

       System.Data.DataTable dtData;
       string cacheKey = "table_data";

        if (Cache.Get(cacheKey) != null)
            dtData = (System.Data.DataTable)Cache.Get(cacheKey);
        else
        {
            dtData = QueryData;
        
            Cache.Insert(cacheKey, dtData, null, DateTime.Now.AddHours(26), TimeSpan.Zero);
        }
Idilio
  • 29
  • 4
  • Did you read [the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.web.caching.cache?view=netframework-4.8)? The cache remains valid as long as the application domain remains active. IIS recycles app pools when idle. That would clear anything in memory. You'd have to persist your cache somewhere outside of app memory if you want to hang around between recycles. Redis is the common platform for that. – mason Mar 25 '21 at 19:38
  • @mason yes I read that documentation namely that sentence, but cannot understand what is the application domain in this context... As this is a server (IIS) a multiple client (webpages) does this mean if I close the webpage, cache is deleted? cannot understand that part... – Idilio Mar 25 '21 at 20:28
  • No...you can learn about [App Domains here](https://stackoverflow.com/questions/1094478). But basically after a certain amount of time with no requests (configurable in IIS) your application will leave memory and basically not be alive until the next request comes at which point IIS will load your app fresh again. So if you want to have a cache that persists across those recycles, you need to host it outside of your app's memory. Redis, SQL Server, a file system, something. – mason Mar 25 '21 at 22:14
  • Thanks for the direction... I just found that application pool recycling option is set to regular time intervals=120 (what justifies the 2 hours) – Idilio Mar 25 '21 at 22:44

0 Answers0