M
Michael Vanhoutte
I was wondering how multiple threads can access the ASP.NET cache object
safely. Take for example the following code that I got from the ASP.NET Cache
object documentation:
DataView Source = (DataView)Cache["MyData1"];
if (Source == null) {
// Connecting to a database...
// Filling a dataset...
Source = new DataView(ds.Tables["Authors"]);
Cache["MyData1"] = Source;
}
MyDataGrid.DataSource=Source;
MyDataGrid.DataBind();
The following questions come to my mind:
1. What happens if two threads reach the 'if (source == null)'-line
simultaneously.
They could both start refilling the cache. Do I need to use locks or mutexes
myself if I want to prevent that?
2. Suppose that thread 1 executed the 'if (source2 == null)' line and found
that that source2 wasn't null. What if thread 2 executed
'Cache.Remove("MyData1")' after thread 1 performed that test but before it
had to change to actually use the value in MyData1.
I would grealy appreciate any feedback!
Michael
safely. Take for example the following code that I got from the ASP.NET Cache
object documentation:
DataView Source = (DataView)Cache["MyData1"];
if (Source == null) {
// Connecting to a database...
// Filling a dataset...
Source = new DataView(ds.Tables["Authors"]);
Cache["MyData1"] = Source;
}
MyDataGrid.DataSource=Source;
MyDataGrid.DataBind();
The following questions come to my mind:
1. What happens if two threads reach the 'if (source == null)'-line
simultaneously.
They could both start refilling the cache. Do I need to use locks or mutexes
myself if I want to prevent that?
2. Suppose that thread 1 executed the 'if (source2 == null)' line and found
that that source2 wasn't null. What if thread 2 executed
'Cache.Remove("MyData1")' after thread 1 performed that test but before it
had to change to actually use the value in MyData1.
I would grealy appreciate any feedback!
Michael