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?
The index cache is enabled by default and can be disabled by specifying the -o no_library_cache option.
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With