Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cache not working as expected when using list(of integer)

Reply
Thread Tools

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

 
 
mick0987b
Guest
Posts: n/a
 
      07-26-2010
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>
 
Reply With Quote
 
 
 
 
mick0987b
Guest
Posts: n/a
 
      07-27-2010
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" <(E-Mail Removed)> wrote in message
>
> news:(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 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 -


 
Reply With Quote
 
 
 
 
mick0987b
Guest
Posts: n/a
 
      07-27-2010
On 27 July, 17:34, "Brian Cryer" <not.here@localhost> wrote:
> "mick0987b" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<newbie> using cache when objects in cache is modified Jeff ASP .Net 2 10-16-2008 05:12 PM
To cache or not to cache David Lozzi ASP .Net 3 08-21-2007 03:40 PM
no-cache - need to reload page from server and not the cache John HTML 2 10-29-2006 05:35 PM
Cache not working as expected Steve Franks ASP .Net 1 09-11-2005 05:05 AM
View source while using Cache-Control: no-cache in frames Yaron Cohen Javascript 0 11-19-2003 10:06 AM



Advertisments