In ASP.NET, is there any reason not to make a set of functions that Add/Remove/Get from the Cache object Static?
Get() - just gets the item, no reason not to be static
Add(), Remove() - I've read that adding/deleting into the cache has it's own internal locking mechanism, so they can be static without me creating my own lock(){} wrapping.
Here is what you need to remember while caching static resources on CDN or local cache server: Use Cache-control HTTP directive to control who can cache the response, under which conditions, and for how long. Configure your server or application to send validation token Etag. Do not cache HTML in the browser.
Static caching is when a browser requests a resource, the server providing the resource can tell the browser how long it should temporarily store or cache the resource. For any subsequent request for that resource, the browser uses its local copy, rather than going to the network to fetch it.
In computing, a cache is a high-speed data storage layer which stores a subset of data, typically transient in nature, so that future requests for that data are served up faster than is possible by accessing the data's primary storage location.
They can already be accessed in a static context through
HttpRuntime.Cache
The method of HttpContext.Current.Cache
merely forwards to this call anyway, yet invoking the HttpContext.Current.Cache
can cause runtime errors if it's not in the lifecycle where HttpContext.Current
is available.
Answering your question:
Yes you could use this to handle that. You would have something like
public static class StaticCache
{
public static Add(object obj)
{
try {
HttpRuntime.Cache.Add(obj);
}
catch(Exception ex) {
//log or something
}
}
}
And usage would be similar to
StaticCache.Add("bob");
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