Accessing ASP.NET Cache from multiple threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Vanhoutte

    Accessing ASP.NET Cache from multiple threads

    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.Tab les["Authors"]);
    Cache["MyData1"] = Source;
    }
    MyDataGrid.Data Source=Source;
    MyDataGrid.Data Bind();


    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
  • Michael Vanhoutte

    #2
    RE: Accessing ASP.NET Cache from multiple threads

    I just replied myself so that it is also posted in
    dotnet.framewor k.aspnet.cachin g. This newsgroups was probably a better place
    to post this question. This was my question:

    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.Tab les["Authors"]);
    Cache["MyData1"] = Source;
    }
    MyDataGrid.Data Source=Source;
    MyDataGrid.Data Bind();


    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

    Comment

    • joker

      #3
      Re: Accessing ASP.NET Cache from multiple threads

      This news group is for classic ASP none of the .net stuff. I'd suggest
      posting in a newsgroup that includes .net in the name for .net problems.

      Michael Vanhoutte wrote:
      [color=blue]
      > I just replied myself so that it is also posted in
      > dotnet.framewor k.aspnet.cachin g. This newsgroups was probably a better place
      > to post this question. This was my question:
      >
      > 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.Tab les["Authors"]);
      > Cache["MyData1"] = Source;
      > }
      > MyDataGrid.Data Source=Source;
      > MyDataGrid.Data Bind();
      >
      >
      > 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[/color]

      Comment

      Working...