Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / April 2004

Tip: Looking for answers? Try searching our database.

Multi-threaded access to Cache object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gary Bagen - 29 Mar 2004 19:38 GMT
http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=a1e21baa
ba5303d7&seekm=8b702e36.0403261601.32f47810%40posting.google.com#link2


The poster of this thread got me thinking. He says that "Any time you
can have multiple threads, especially multiple page requests,
accessing the same object, you have to handle any synchronization
issues."

However, any example I see of accessing the Cache (or Application and
Session objects for that matter) typically show this snippet:

  Dim dsProducts As DataSet

  dsProducts = CType(HttpRuntime.Cache.Get("dsProducts"), DataSet)

  If dsProducts Is Nothing Then
     dsProducts = GetProductsFromDatabase()
     
     HttpRuntime.Cache.Insert("dsProducts", dsProducts)
  End If

Now the above example is coded for global.asax but the same would
apply for a page. (Page.Cache instead of HttpRuntime.Cache).

So here is the synchronization issue. Many threads can simultaneously
execute this code snippet (either from the Global class for
HttpRuntime.Cache or from multiple instances of a Page class for
Page.Cache). My understanding is that the Cache object is
multithreaded so we are okay there. However, simultaneous threads can
be reading "dsProducts Is Nothing" and calling "dsProducts =
GetProductsFromDatabase()". The fact that "dsProducts =
GetProductsFromDatabase()" could take a few cycles certainly opens the
door for other threads before the first dsProducts is inserted into
the Cache.

I think the application would still work though. It would just be that
many threads would read from the database and insert into the Cache.
Since the Cache would use a writer lock it would just line up the
.Inserts in sequential access. We would just be doing redundant work.

I think this could be solved by working with the
System.Threading.ReaderWriterLock class and surround the code snippet
above with synchronization to block all reads. This probably
introduces scalability issues from so much blocking since most of the
time the Cache should be full. So that means that doing some redundant
work during the load of the Cache is probably an acceptable tradeoff.

I was wondering if my theories are correct or if I am off my rockers.
John Saunders - 29 Mar 2004 20:39 GMT
http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=a1e21baa
ba5303d7&seekm=8b702e36.0403261601.32f47810%40posting.google.com#link2


> The poster of this thread got me thinking.

That's always a good thing. :-)

> He says that "Any time you
> can have multiple threads, especially multiple page requests,
[quoted text clipped - 13 lines]
>       HttpRuntime.Cache.Insert("dsProducts", dsProducts)
>    End If

The problem with the above isn't the redundant work in inserting a DataSet
into the Cache. The problem is that each of the threads doing the insert
could be operating on a different DataSet.

I believe that Application_Start will only execute once per application
instance. The only way I can see for you to get into trouble there would be
in some pathological case where the web application resets while your code
is still executing Application_Start. If that's guaranteed by Microsoft to
be impossible, then you should be ok.

The problem would come in if you had the above code on a page. In that case,
without any guarantees on how many threads can execute the code at the same
time, you could wind up two threads inserting and using different DataSets.
Signature

John Saunders
John.Saunders at SurfControl.com

Gary Bagen - 30 Mar 2004 00:14 GMT
Good point John. I will stipulate that the data going into the Cache
must be the same for all users. The Session would be for the user
specific data.

Gar

>  http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=a1e21baa
ba5303d7&seekm=8b702e36.0403261601.32f47810%40posting.google.com#link2

> >
[quoted text clipped - 33 lines]
> without any guarantees on how many threads can execute the code at the same
> time, you could wind up two threads inserting and using different DataSets.
coollzh - 01 Apr 2004 13:24 GMT
but if my Cache have been Invalidate, i must refresh my Cache.at this point,
mutil threads maybe refresh the Cache of Dataset?? i think we should make
some  synchronization  on  modifying Cache.

"John Saunders" <john.saunders at SurfControl.com> ????????????
:#U2iOWcFEHA.2052@TK2MSFTNGP11.phx.gbl...

http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=a1e2
1baaba5303d7&seekm=8b702e36.0403261601.32f47810%40posting.google.com#link2

> > The poster of this thread got me thinking.
>
[quoted text clipped - 34 lines]
> John Saunders
> John.Saunders at SurfControl.com
John Saunders - 01 Apr 2004 21:13 GMT
> but if my Cache have been Invalidate, i must refresh my Cache.at this point,
> mutil threads maybe refresh the Cache of Dataset?? i think we should make
> some  synchronization  on  modifying Cache.

Good point. I forgot about invalidation.

If ASP.NET doesn't guarantee that the CacheItemRemovedCallback locks the
Cache, then you will need to lock it yourself.
Signature

John Saunders
John.Saunders at SurfControl.com

> "John Saunders" <john.saunders at SurfControl.com> ????????????
> :#U2iOWcFEHA.2052@TK2MSFTNGP11.phx.gbl...

http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=a1e2
> 1baaba5303d7&seekm=8b702e36.0403261601.32f47810%40posting.google.com#link2
> > >
[quoted text clipped - 40 lines]
> > John Saunders
> > John.Saunders at SurfControl.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.