Cache

D

Dave

I am currently having a problem with retaining the cache in my asp.net
application. I am trying to insert a small DataView into cache, then
recall it on various pages throughout the site. When on any page other
than the page that inserted the cache, the DataView is null (not the
cache object, but the DataView itself). I'm curious as to if my code
is wrong, or if the DataView wasn't meant to be placed into cache.

// Code for insert
System.Web.HttpContext.Current.Cache.Insert(
"key",
dataView,
null,
DateTime.Now.AddHours(1),
TimeSpan.Zero
);

// Code for recall
DataView dv = (DataView)System.Web.HttpContext.Current.Cache[key];

Am I doing anything wrong? Might there be an entry that I would need
to add to my web.config to enable the cache? And on a side note, is
there a limit to the amount of characters placed in the key string?

Thank you,
Dave
 
C

Chris R. Timmons

(e-mail address removed) (Dave) wrote in
I am currently having a problem with retaining the cache in my
asp.net application. I am trying to insert a small DataView into
cache, then recall it on various pages throughout the site. When
on any page other than the page that inserted the cache, the
DataView is null (not the cache object, but the DataView
itself). I'm curious as to if my code is wrong, or if the
DataView wasn't meant to be placed into cache.

// Code for insert
System.Web.HttpContext.Current.Cache.Insert(
"key",
dataView,
null,
DateTime.Now.AddHours(1),
TimeSpan.Zero
);

// Code for recall
DataView dv =
(DataView)System.Web.HttpContext.Current.Cache[key];

Am I doing anything wrong? Might there be an entry that I would
need to add to my web.config to enable the cache? And on a side
note, is there a limit to the amount of characters placed in the
key string?

Dave,

It looks OK. Just make sure you are using the same key value. In
your Insert code you are using a "key" string literal. When
retrieving the value your code uses a string variable named key. If
the variable key does not contain the value "key" when trying to
recall the DataView, null will be returned.

Hope this helps.

Chris.
 
D

Dave

Dave,
It looks OK. Just make sure you are using the same key value. In
your Insert code you are using a "key" string literal. When
retrieving the value your code uses a string variable named key. If
the variable key does not contain the value "key" when trying to
recall the DataView, null will be returned.

Hope this helps.

Chris.


Cris,

Thank you for your response. I am using the proper keys for setting
and retrieving of the cached data. For my code example I just replaced
my variable with the string "key". There isn't any web.config or
special server settings to enable the cache, right?

Thanks,
Dave
 
D

Dave

I figured out my problem. =) I have a habbit of disposing of my
dataviews after I "no longer need them". In this case I was disposing
of it after using it in my control. This now brings up the question of
proper clean up.

Will .Net's garbage collection properly release the memory consumed by
the dataview when the cache expires?

Thanks,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top