From: mick0987b on
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>
From: Brian Cryer on
"mick0987b" <miketayloruk(a)gmail.com> wrote in message
news:28804f08-79db-47e7-8e72-b8c632895ed6(a)d8g2000yqf.googlegroups.com...
> 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

From: mick0987b on
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(a)localhost> wrote:
> "mick0987b" <miketaylo...(a)gmail.com> wrote in message
>
> news:28804f08-79db-47e7-8e72-b8c632895ed6(a)d8g2000yqf.googlegroups.com...
>
>
>
>
>
> > 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 -

From: Brian Cryer on

"mick0987b" <miketayloruk(a)gmail.com> wrote in message
news:ec218b6a-4d91-493a-9e9c-ca7d96c06702(a)f8g2000vbl.googlegroups.com...
> 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

From: mick0987b on
On 27 July, 17:34, "Brian Cryer" <not.here(a)localhost> wrote:
> "mick0987b" <miketaylo...(a)gmail.com> wrote in message
>
> news:ec218b6a-4d91-493a-9e9c-ca7d96c06702(a)f8g2000vbl.googlegroups.com...
>
> > 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