Accessing ASP.NET Cache from multiple threads

Discussion in 'ASP General' started by Michael Vanhoutte, Aug 21, 2004.

  1. 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
    Michael Vanhoutte, Aug 21, 2004
    #1
    1. Advertising

  2. I just replied myself so that it is also posted in
    dotnet.framework.aspnet.caching. 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.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
    Michael Vanhoutte, Aug 21, 2004
    #2
    1. Advertising

  3. Michael Vanhoutte

    joker Guest

    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:

    > I just replied myself so that it is also posted in
    > dotnet.framework.aspnet.caching. 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.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
    joker, Aug 21, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daren

    Accessing ASP.NET Cache

    Daren, Apr 30, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    2,034
    Ashish M Bhonkiya
    May 2, 2004
  2. =?Utf-8?B?b25l?=
    Replies:
    1
    Views:
    5,283
    Karl Seguin [MVP]
    Mar 8, 2006
  3. Smegly
    Replies:
    1
    Views:
    1,096
    Mitchell
    May 19, 2004
  4. Sergey via DotNetMonster.com

    ASP.NET Cache vs Window System Cache

    Sergey via DotNetMonster.com, Nov 15, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    475
    Sergey via DotNetMonster.com
    Nov 15, 2006
  5. Jag
    Replies:
    2
    Views:
    255
    RedGrittyBrick
    Nov 21, 2008
Loading...

Share This Page