Cache not working as expected when using list(of integer)

Discussion in 'ASP .Net' started by mick0987b, Jul 26, 2010.

  1. mick0987b

    mick0987b Guest

    Hi,

    Please can someone explain what is happeing here. I'm caching a list
    of integers but they appear to dissapear when the original values are
    changed even though there is no dependencies set up. Example code
    below. Why does the cache dissapear?

    <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Dim my_int As Integer
    Dim mylist As New List(Of Integer)
    Dim myotherlist As New List(Of Integer)
    Dim myfinallist As New List(Of Integer)



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs)

    Response.Write("Adding value to my_int - [my_int=42]<br />")
    my_int = 42
    Response.Write("caching value of my_int
    [cache('my_cached_int')=my_int<br />")
    Cache("my_cached_int") = my_int
    Response.Write("Clearing value of my_int - [my_int=0]<br />")
    my_int = 0
    Response.Write("Getting value back from cache (I expect it
    still to be 42)<br />")
    Response.Write("My_cached_int = " &
    Cache("my_cached_int").ToString & "<br />")
    Response.Write("<br /><br />OK that works fine<br />")
    Response.Write("This time use a 'list of' integers<br />")
    Response.Write("mylist is a list(of integers)<br />")
    Response.Write("Adding some values, 34,65,32<br />")
    mylist.Add(34)
    mylist.Add(65)
    mylist.Add(32)
    Response.Write("caching the list to 'my_cached_list'<br />")
    Cache("my_cached_list") = mylist
    Response.Write("retrieve the list and check count<br />")
    myotherlist = Cache("my_cached_list")
    Response.Write("got " & myotherlist.Count.ToString & " items
    (as expected)<br />")
    Response.Write("NOW..clear the original list, not the cache,
    just the list [mylist.clear()] <br />")
    mylist.Clear()
    Response.Write("check the count of the cached list again <br /
    >")

    myfinallist = Cache("my_cached_list")
    Response.Write("got " & myfinallist.Count.ToString & " items
    (WHY HAS THE CACHE CHANGED??)<br />")

    End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
    </body>
    </html>
     
    mick0987b, Jul 26, 2010
    #1
    1. Advertising

  2. mick0987b

    mick0987b Guest

    Hi Brian,

    Thanks for your response, this explains what is happening.

    I'm stil a little confused though, my understanding was that I could
    use cache to store data to improve performance, so storing only a
    reference to that data seems of little value?

    Is there a way to store the actual values of lists/arraylist to cache?
    I have an arraylist that is compiled after making many queries to my
    database, obviously I dont want to build this array every page
    refresh, how do people generally cache in this situation?

    Thanks again.

    Mick






    On 27 July, 10:43, "Brian Cryer" <not.here@localhost> wrote:
    > "mick0987b" <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > Hi,

    >
    > > Please can someone explain what is happeing here. I'm caching a list
    > > of integers but they appear to dissapear when the original values are
    > > changed even though there is no dependencies set up. Example code
    > > below. Why does the cache dissapear?

    >
    > > <%@ Page Language="VB" %>

    >
    > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    > >www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    >
    > > <script runat="server">

    >
    > >    Dim my_int As Integer
    > >    Dim mylist As New List(Of Integer)
    > >    Dim myotherlist As New List(Of Integer)
    > >    Dim myfinallist As New List(Of Integer)

    >
    > >    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > > System.EventArgs)

    >
    > >        Response.Write("Adding value to my_int - [my_int=42]<br />")
    > >        my_int = 42
    > >        Response.Write("caching value of my_int
    > > [cache('my_cached_int')=my_int<br />")
    > >        Cache("my_cached_int") = my_int
    > >        Response.Write("Clearing value of my_int  - [my_int=0]<br />")
    > >        my_int = 0
    > >        Response.Write("Getting value back from cache (I expect it
    > > still to be 42)<br />")
    > >        Response.Write("My_cached_int = " &
    > > Cache("my_cached_int").ToString & "<br />")
    > >        Response.Write("<br /><br />OK that works fine<br />")
    > >        Response.Write("This time use a 'list of' integers<br />")
    > >        Response.Write("mylist is a list(of integers)<br />")
    > >        Response.Write("Adding some values, 34,65,32<br />")
    > >        mylist.Add(34)
    > >        mylist.Add(65)
    > >        mylist.Add(32)
    > >        Response.Write("caching the list to 'my_cached_list'<br />")
    > >        Cache("my_cached_list") = mylist
    > >        Response.Write("retrieve the list and check count<br />")
    > >        myotherlist = Cache("my_cached_list")
    > >        Response.Write("got " & myotherlist.Count.ToString & " items
    > > (as expected)<br />")
    > >        Response.Write("NOW..clear the original list, not the cache,
    > > just the list [mylist.clear()] <br />")
    > >        mylist.Clear()
    > >        Response.Write("check the count of the cached list again <br /
    > >>")

    > >        myfinallist = Cache("my_cached_list")
    > >        Response.Write("got " & myfinallist.Count.ToString & " items
    > > (WHY HAS THE CACHE CHANGED??)<br />")

    >
    > I assume you get 0 here?
    >
    > This comes down to the difference between object types and non-object types.
    > An integer is not an object type, so when you assign you are creating a new
    > COPY of the integer. A list is an object so when you assign you are creating
    > another REFERENCE to the same object.
    >
    > So the answer is that the cache hasn't changed, but you have changed the
    > object.
    >
    > The cache does not store a copy of your list, but a reference to the object.
    > So both the cache and "my_cache_list" both refer to the same object - not
    > separate copies of it but the same object. Modify either one and you modify
    > the same object. So where you have mylist.Clear() you could have replaced
    > that with cache("my_cached_list").Clear(), because both refer to the same
    > object in this case a list.
    >
    > This is why it behaves differently when you store an integer in the cache..
    > With an integer is not an object so when you assign it to another variable
    > or put it in the cache you are creating a separate copy of it.  This is why
    > you could change it and the cached copy is different, because when dealing
    > with integers its a cached COPY. When dealing with objects is a cached
    > REFERENCE.
    >
    > If instead of your integer you used say Nullable(of Int) then I think you'd
    > find it behaved the same way as with your list, because if you used
    > Nullable(of Int) you are then passing around an object.
    >
    > Hope this helps.
    > --
    >  Brian Cryer
    >  http://www.cryer.co.uk/brian- Hide quoted text -
    >
    > - Show quoted text -
     
    mick0987b, Jul 27, 2010
    #2
    1. Advertising

  3. mick0987b

    mick0987b Guest

    On 27 July, 17:34, "Brian Cryer" <not.here@localhost> wrote:
    > "mick0987b" <> wrote in message
    >
    > news:...
    >
    > > Hi Brian,

    >
    > > Thanks for your response, this explains what is happening.

    >
    > > I'm stil a little confused though, my understanding was that I could
    > > use cache to store data to improve performance, so storing only a
    > > reference to that data seems of little value?

    >
    > Because if you store it in the cache then (subject to it having been dropped
    > from the cache etc) it will still be there on the next post-back. All the
    > variables on a page need to be recreated on each postback, so by sticking it
    > in the cache it is there for next time. Just be aware that it might have
    > been removed from the cache, and that the cache is global - all sessions
    > share the same cache. So as a rule don't store session specific information
    > in the cache.
    >
    > > Is there a way to store the actual values of lists/arraylist to cache?
    > > I have an arraylist that is compiled after making many queries to my
    > > database, obviously I dont want to build this array every page
    > > refresh, how do people generally cache in this situation?

    >
    > You probably don't want to, and that its using a reference is what you want.
    > This means that it is available on the next post-back/page-refresh. The
    > garbage collector will only destroy the list when the last reference to it
    > has gone, so whilst a reference remains in the cache the list will remain..
    >
    > Does that help?
    > --
    >  Brian Cryer
    >  http://www.cryer.co.uk/brian


    Yes Brian, that helps a lot, my understanding of cache was slightly
    off but I'm getting it now.
    I just bookmarked your website, thank you for your time :)

    Mick
     
    mick0987b, Jul 27, 2010
    #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. Steve Franks

    Cache not working as expected

    Steve Franks, Sep 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    475
    Jeremy S.
    Sep 11, 2005
  2. John
    Replies:
    2
    Views:
    1,216
  3. David Lozzi

    To cache or not to cache

    David Lozzi, Aug 21, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    372
    vMike
    Aug 21, 2007
  4. Jeff
    Replies:
    2
    Views:
    703
  5. Yaron Cohen
    Replies:
    0
    Views:
    347
    Yaron Cohen
    Nov 19, 2003
Loading...

Share This Page