ASP.NET cache object thread safe?

G

Guest

Hi

I have a N-tier ASP.NET application that uses a data access tier to get data
from a database and pass it to the middleware/business tier for
processing/filtering and then passes the modified data to the web tier for
presentaion.

What I would like to do is introduce a cache to the middleware tier to cut
down on the round trips to the database.

The question I have is this. Is the cache threadsafe? I will be accessing it
from two different places in the middleware tier. One to read the cache, the
other to update the cache.

Reading newsgroups I am getting conflicting messages as to whether the cache
is thread safe or not.

I'd appreciate any advice,

Thanks
Macca
 
N

Nicholas Paldino [.NET/C# MVP]

Macca,

Well, the documentation for the Cache class in the System.Web.Caching
namespace declares that the instance is safe for multi-threading. Of
course, this makes sense, given that it is going to be hit by a number of
threads that are processing page requests.

However, I would recommend that you not use the cache in your
middleware. This ties your middleware to ASP.NET, which in general, isn't a
good idea. It doesn't make your routines portable at all, in case you
decide to access your middleware through other means.

I would recommend taking a look at the enterprise library:

http://msdn.microsoft.com/library/?url=/library/en-us/dnpag2/html/EntLib2.asp

In it, I would take a look at the caching application block, which
actually has more features than ASP.NET's caching mechanism:

http://msdn.microsoft.com/practices...?pull=/library/en-us/dnpag2/html/caching1.asp

I would also assume that this is thread safe, but if you don't trust it,
the code for the application block is distributed with the library, so you
can make it thread safe, if you wish or you find that it isn't.

Hope this helps.
 

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

Top