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

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

Post by mick0987 » Wed, 28 Jul 2010 05:05:07


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.yqcomputer.com/ ">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
 
 
 

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

Post by Brian Crye » Wed, 28 Jul 2010 18:43:44

mick0987b" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

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


 
 
 

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

Post by mick0987 » Thu, 29 Jul 2010 00:24:29

i 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:
> > rotected Sub Page_Load(ByVal sender As Object, ByVal e>A>
> > System.EventAr>s)> > > esponse.Write("Adding value to my_int - [my_in<=>2]")
> > y>i>t = 42
> > esponse.Write("caching val>e>of my_int
> > [cache('my_cached_int><=my>i>t
")
> > ache("my_cached>i>t") = my_int
> > esponse.Write("Clearing value of my_i>< >[>y_int=0]
") >>>> y_int = 0
> > esponse.Write("Getting value bac> >rom cache (I expect i>< > > >till to be 42)
")
> > espons>.>rite("My_cached_int = " &
> > Cache("my_><che>_>nt").ToString & "
") &<t;> > <t;esponse.Write("<>r>/>OK that works fine
")
> > esponse.Write("Thi><tim> >se a 'list of' integers
")
> > esponse.Wr><e(">y>ist is a list(of integers)
")
> > espons>.Wri&g<;e>"Adding some values, >4>65,32
")
> > > > ylist.Add(34)
> >>gt;ylist.Add(65)
> > ylist.Add(32)
> > es>onse>W>ite("caching the list to 'my_cached_lis>'>br />")
> > ache("my_cached_list") = mylist
> > gt;><>sponse.Write("retrieve the list and check co>n>
")
> > yotherlist = Cache("my_cached_list")
> > gt;gt;esponse.Write("go> "<&>m>otherlist.Count.ToString & " items
> > (as expected)
")
> > > > esponse.Write("NOW..clear the or>gina> &<t;ist, not the cache,
> > ylist.Clear()
> >>")
> > yfinallist = Cache("my_cached_>i>t")
> > esponse.Write("got>" & >yf&<t;nallist.Count.ToString & " >te>s
> > (WHY HAS THE CACHE CHANGED??)
")
>
> 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 obje>t >o when you assign you are creating
> another REFERENCE to the same object.> >
> So t>e >nswer 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 objec>.
> 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 modi>y
> the same object. So where you have mylist.Clear() you could have replac>d
> that with cache("my_cache>_l>st").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. his is wh>
> you could change it and the cached copy is different, because when de&g
 
 
 

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

Post by Brian Crye » Thu, 29 Jul 2010 01:34:48


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.


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.yqcomputer.com/
 
 
 

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

Post by mick0987 » Thu, 29 Jul 2010 05:52:52


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