Use static class for caching, any negatives?

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

It seems to me that using static classes for caching as versus the
Application object has certain advantages. First, the cached values are
strongly typed, secondly, it's not ASP.NET specific so the same code can be
used for other .NET apps. I can't think of anything bad about it. I only
plan to use it for a small number of non-volatile values (but needs to be
read from DB or other sources once) so it's like I'd put everything in a
static class.

Here's a piece of sample code:

Class App {
private static XslTransform _menuXsl;

public XslTransform {
get { return _menuXsl; }
set { _menuXsl = value; }
}
}
 
HI Bob:

You certainly make good point. Just remember the Application object
does a little extra work for you by synchronizing access to the
collection using a reader / writer lock. This will prevent corruption
of the container, and allows a little more throughput than using a
regular lock / mutex.
 
Hi Bob:

Some of the classes in the .NET base class library have static members
which are thread safe, but not all. Anything we would write ourselves
would not be thread safe unless we add the code to make it safe!

--s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top