Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Cache access methods static

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.

like image 205
eych Avatar asked Nov 12 '10 15:11

eych


People also ask

How do you set cache control for static content?

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.

What is static caching?

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.

What is caching in computer?

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.


1 Answers

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");

like image 146
Chris Marisic Avatar answered Oct 06 '22 00:10

Chris Marisic