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" <miketaylo...@gmail.com> wrote in message
>
> news:28804f08-79db-47e7-8e72-(E-Mail Removed)...
>
>
>
>
>
> > 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 iswhy
> 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 -