Cache dataset and expire

Discussion in 'ASP .Net' started by martin, Oct 15, 2003.

  1. martin

    martin Guest

    Hi,

    I am storing a dataset in cache, which is happening fine. I can easily
    retrive it at postback from the cache, cast it to a dataset and reuse it.
    However I have specified that the cache expire in 5 minutes like so.

    If Not IsPostBack Then
    BindMyDropDown()
    Else
    Response.Write("<hr>Cache Expires 5 minutes" &
    DateTime.Now.AddSeconds(300) & "<hr>")
    'If the cache had expired I would expect this line to cause an
    error -- but it doesn't even if I cause postback after 15 minutes!!!
    dsMyDataset = CType(Cache("ds"), DataSet)
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    End If

    I also set the cache expire in exactly the same way in the BindMyDropDown()
    function.
    and place the dataset into the cache like so.

    //code here
    objConn.Open()
    daMyDataAdapter.Fill(dsMyDataset, "Customers")
    objConn.Close()
    Cache.Insert("ds", dsMyDataset)
    Response.Write("<hr>Cache Expires 5 minutes" &
    DateTime.Now.AddSeconds(300) & "<hr>")
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))

    DropDownList1.DataSource = dsMyDataset
    DropDownList1.DataValueField =
    dsMyDataset.Tables(0).Columns(0).ToString
    DropDownList1.DataTextField =
    dsMyDataset.Tables(0).Columns(1).ToString
    DropDownList1.DataBind()

    when a button is clicked and a postback is caused the button handler
    retrieves the cache dataset and rebinds it to a combo box.
    I would have thought that if the page was posted back any later than 5
    minutes that an error would occur because the cache had expired, but it
    doesn't.
    The lack of this error indicates to me that the cache is not expiring, but I
    do not know what I am doing wrong.
    How do I make the cache expire in 5 minutes.

    cheers

    martin
     
    martin, Oct 15, 2003
    #1
    1. Advertising

  2. You may be pulling some info from ViewState, which would pull the values
    despite the cache being refreshed. Turn off ViewState to test this behavior.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    **********************************************************************
    Think Outside the Box!
    **********************************************************************
    "martin" <> wrote in message
    news:...
    > Hi,
    >
    > I am storing a dataset in cache, which is happening fine. I can easily
    > retrive it at postback from the cache, cast it to a dataset and reuse it.
    > However I have specified that the cache expire in 5 minutes like so.
    >
    > If Not IsPostBack Then
    > BindMyDropDown()
    > Else
    > Response.Write("<hr>Cache Expires 5 minutes" &
    > DateTime.Now.AddSeconds(300) & "<hr>")
    > 'If the cache had expired I would expect this line to cause an
    > error -- but it doesn't even if I cause postback after 15 minutes!!!
    > dsMyDataset = CType(Cache("ds"), DataSet)
    > Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    > End If
    >
    > I also set the cache expire in exactly the same way in the

    BindMyDropDown()
    > function.
    > and place the dataset into the cache like so.
    >
    > //code here
    > objConn.Open()
    > daMyDataAdapter.Fill(dsMyDataset, "Customers")
    > objConn.Close()
    > Cache.Insert("ds", dsMyDataset)
    > Response.Write("<hr>Cache Expires 5 minutes" &
    > DateTime.Now.AddSeconds(300) & "<hr>")
    > Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    >
    > DropDownList1.DataSource = dsMyDataset
    > DropDownList1.DataValueField =
    > dsMyDataset.Tables(0).Columns(0).ToString
    > DropDownList1.DataTextField =
    > dsMyDataset.Tables(0).Columns(1).ToString
    > DropDownList1.DataBind()
    >
    > when a button is clicked and a postback is caused the button handler
    > retrieves the cache dataset and rebinds it to a combo box.
    > I would have thought that if the page was posted back any later than 5
    > minutes that an error would occur because the cache had expired, but it
    > doesn't.
    > The lack of this error indicates to me that the cache is not expiring, but

    I
    > do not know what I am doing wrong.
    > How do I make the cache expire in 5 minutes.
    >
    > cheers
    >
    > martin
    >
    >
    >
    >
     
    Cowboy \(Gregory A. Beamer\), Oct 15, 2003
    #2
    1. Advertising

  3. martin

    vMike Guest

    I am not sure about datasets but with datatables and dataviews, if the cache
    is empty it just returns nothing but does not generate an error. You can see
    exactly what is in the cache by using the following

    dim strCacheContents as String
    dim objItem as DictionaryEntry
    dim strName as String
    For Each objItem In Cache
    strName = objItem.Key.tostring()
    strCacheContents = "key=" & strName & "<br />"
    Response.Write(strCacheContents)
    strCacheContents = "value=" & Convert.ToString(objItem.Value) & "<br/>"
    Response.Write(strCacheContents)

    Next


    "martin" <> wrote in message
    news:...
    > Hi,
    >
    > I am storing a dataset in cache, which is happening fine. I can easily
    > retrive it at postback from the cache, cast it to a dataset and reuse it.
    > However I have specified that the cache expire in 5 minutes like so.
    >
    > If Not IsPostBack Then
    > BindMyDropDown()
    > Else
    > Response.Write("<hr>Cache Expires 5 minutes" &
    > DateTime.Now.AddSeconds(300) & "<hr>")
    > 'If the cache had expired I would expect this line to cause an
    > error -- but it doesn't even if I cause postback after 15 minutes!!!
    > dsMyDataset = CType(Cache("ds"), DataSet)
    > Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    > End If
    >
    > I also set the cache expire in exactly the same way in the

    BindMyDropDown()
    > function.
    > and place the dataset into the cache like so.
    >
    > //code here
    > objConn.Open()
    > daMyDataAdapter.Fill(dsMyDataset, "Customers")
    > objConn.Close()
    > Cache.Insert("ds", dsMyDataset)
    > Response.Write("<hr>Cache Expires 5 minutes" &
    > DateTime.Now.AddSeconds(300) & "<hr>")
    > Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    >
    > DropDownList1.DataSource = dsMyDataset
    > DropDownList1.DataValueField =
    > dsMyDataset.Tables(0).Columns(0).ToString
    > DropDownList1.DataTextField =
    > dsMyDataset.Tables(0).Columns(1).ToString
    > DropDownList1.DataBind()
    >
    > when a button is clicked and a postback is caused the button handler
    > retrieves the cache dataset and rebinds it to a combo box.
    > I would have thought that if the page was posted back any later than 5
    > minutes that an error would occur because the cache had expired, but it
    > doesn't.
    > The lack of this error indicates to me that the cache is not expiring, but

    I
    > do not know what I am doing wrong.
    > How do I make the cache expire in 5 minutes.
    >
    > cheers
    >
    > martin
    >
    >
    >
    >
     
    vMike, Oct 15, 2003
    #3
  4. martin

    vMike Guest

    You might also use this syntax.

    Cache.Insert ("yourname",yourdatasetvariable,New CacheDependency(filepath)
    or Nothing ,DateTime.MaxValue, TimeSpan.FromMinutes(5))

    I am not sure but I thought the response.cache ... applies to the whole
    control or page not the item being named in the cache insert line, but I am
    not expert. When I have used the response.cache I usually have several
    other lines as follows (but this is for an entire page)

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(intCacheTime))
    Response.Cache.SetCacheability(HttpCacheability.Public)
    Response.Cache.SetValidUntilExpires(True)
    Response.Cache.VaryByParams("_p") = True
    Response.Cache.VaryByParams("_h") = True
    Response.Cache.VaryByParams("_c") = True

    "martin" <> wrote in message
    news:...
    > Hi,
    >
    > I am storing a dataset in cache, which is happening fine. I can easily
    > retrive it at postback from the cache, cast it to a dataset and reuse it.
    > However I have specified that the cache expire in 5 minutes like so.
    >
    > If Not IsPostBack Then
    > BindMyDropDown()
    > Else
    > Response.Write("<hr>Cache Expires 5 minutes" &
    > DateTime.Now.AddSeconds(300) & "<hr>")
    > 'If the cache had expired I would expect this line to cause an
    > error -- but it doesn't even if I cause postback after 15 minutes!!!
    > dsMyDataset = CType(Cache("ds"), DataSet)
    > Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    > End If
    >
    > I also set the cache expire in exactly the same way in the

    BindMyDropDown()
    > function.
    > and place the dataset into the cache like so.
    >
    > //code here
    > objConn.Open()
    > daMyDataAdapter.Fill(dsMyDataset, "Customers")
    > objConn.Close()
    > Cache.Insert("ds", dsMyDataset)
    > Response.Write("<hr>Cache Expires 5 minutes" &
    > DateTime.Now.AddSeconds(300) & "<hr>")
    > Response.Cache.SetExpires(DateTime.Now.AddSeconds(300))
    >
    > DropDownList1.DataSource = dsMyDataset
    > DropDownList1.DataValueField =
    > dsMyDataset.Tables(0).Columns(0).ToString
    > DropDownList1.DataTextField =
    > dsMyDataset.Tables(0).Columns(1).ToString
    > DropDownList1.DataBind()
    >
    > when a button is clicked and a postback is caused the button handler
    > retrieves the cache dataset and rebinds it to a combo box.
    > I would have thought that if the page was posted back any later than 5
    > minutes that an error would occur because the cache had expired, but it
    > doesn't.
    > The lack of this error indicates to me that the cache is not expiring, but

    I
    > do not know what I am doing wrong.
    > How do I make the cache expire in 5 minutes.
    >
    > cheers
    >
    > martin
    >
    >
    >
    >
     
    vMike, Oct 15, 2003
    #4
    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. Dmitry Bond.
    Replies:
    0
    Views:
    529
    Dmitry Bond.
    Aug 31, 2005
  2. Dicky Cheng

    absolute expiration doesn't expire cache

    Dicky Cheng, Aug 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    516
    Dicky Cheng
    Aug 16, 2004
  3. kurt sune

    Expire IE browser cache, again.

    kurt sune, Oct 17, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    5,700
    kurt sune
    Oct 18, 2005
  4. Frank Rizzo

    How to set output cache to never expire?

    Frank Rizzo, Feb 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,320
    Joerg Jooss
    Feb 20, 2006
  5. Tongass Park Neighborhood Association, Juneau Alas

    Cookies expire immediately, not when set to expire

    Tongass Park Neighborhood Association, Juneau Alas, Oct 1, 2009, in forum: ASP General
    Replies:
    2
    Views:
    1,204
    SQLDude
    Nov 24, 2009
Loading...

Share This Page