Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how long, by default does stuff stay in httpcache if i don't put an explicit expiration?

i have the following code to cache some expensive code.

  private MyViewModel GetVM(Params myParams)
    {
        string cacheKey = myParams.runDate.ToString();
        var cacheResults = HttpContext.Cache[cacheKey] as MyViewModel ;
        if (cacheResults == null)
        {
            cacheResults = RunExpensiveCodeToGenerateVM(myParams);
            HttpContext.Cache[cacheKey] = cacheResults;
        }                
   return cacheResults;
   }

will this stay in the cache forever? until the server reboots or runs out of memory?

like image 424
leora Avatar asked Jun 30 '11 11:06

leora


People also ask

Which of the following cache is enabled by default?

The index cache is enabled by default and can be disabled by specifying the -o no_library_cache option.

Should you cache images?

Is cached data important? Cached data isn't inherently important, as it's only considered “temporary storage.” However, it does exist to improve the user experience. On-page elements like images, videos, and even text take some time to load.

How does cache work?

How does Caching work? The data in a cache is generally stored in fast access hardware such as RAM (Random-access memory) and may also be used in correlation with a software component. A cache's primary purpose is to increase data retrieval performance by reducing the need to access the underlying slower storage layer.


1 Answers

will this stay in the cache forever?

This will depend on the particular cache provider you are using. For example if you are using the default in-memory cache it might expire if the server starts running low on memory or if the application pool is recycled. But if you are using some other cache provider, like for example a distributed cache like memcached or AppFactory this will depend on the particular implementation.

The rule of thumb is to never assume that something is inside the cache because you previously stored it. Always check for the presence of the item in the cache first and if not present fetch it and store in the cache again.

like image 152
Darin Dimitrov Avatar answered Nov 11 '22 11:11

Darin Dimitrov