name 'cache' is not declared?

  • Thread starter Thread starter darrel
  • Start date Start date
D

darrel

I'm accessing an XML file several times on a page, and, as such, want to
cache it.

I'm setting it up as such:

if cache("xmlFile") is nothing then...
load file
cache file
else
read it
end if

This works fine in my codebehind, but I'm trying to move this out into a
standalone class so I can call it from various usercontrols. When I do this,
I get an error of "name 'cache' is not declared?'

Is this a namespace issue? I don't declare a more specific namespace in my
codebehind page that uses it (other than System.Web.UI.UserControl), so I'm
confused as to what namespace is missing.

-Darrel
 
You will need to access Cache through the HttpContent.Current property.

if System.Web.HttpContext.Current.Cache( "xmlFile" ) is nothing then
...

HTH,

bill
 
Try including the System.Web.Caching namespace in your standalone code.

I tried that as such:

if System.Web.Caching.cache("footerText") Is Nothing Then

but get this error:

'cache' is a type in 'caching' and can not be used as an expression.

-Darrel
 
VB.NET :
myValue = Cache("mykey")
If Not (myValue Is Nothing) Then
DisplayData(myValue)
End If

C# :
myValue = Cache("mykey");
if(myValue != null ) {
DisplayData(myValue);
}


You could add a dependency with :

VB.NET :
Cache.Insert("MyData", Source, _
New CacheDependency(Server.MapPath("your.xml")))

C# :
Cache.Insert("MyData", Source,
new CacheDependency(Server.MapPath("your.xml")));



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
You will need to access Cache through the HttpContent.Current property.
if System.Web.HttpContext.Current.Cache( "xmlFile" ) is nothing then

Aha! That's it! Thanks!

Now, why do I need to be so explicit when doing this in a standalone class
vs. doing it in on a codebehind page? Also, is there a trick to 'reverse
lookup' the namespace a particular object exists in? I noticed on MSDN, that
there are a variety of cache objects, so I wasn't really finding the
specific namespage I was looking for.

-Darrel
 
When you are coding this in a codebehind page, the page itself has a Cache
property. When the page is being created by the framework, Response,
Request, Session, etc are all pulled from HttpContext.Current and assigned
to the page.

The page (codebehind) just does it automatically for you.

I am not sure what you are asking about a "reverse lookup". Clarify if you
would like an answer.

bill
 
When you are coding this in a codebehind page, the page itself has a Cache
property. When the page is being created by the framework, Response,
Request, Session, etc are all pulled from HttpContext.Current and assigned
to the page.

Well, that makes sense! Thanks for explaining that.
I am not sure what you are asking about a "reverse lookup". Clarify if you
would like an answer.

Well, often I want to use a specific command/object etc. Lots of tutorials
on the web use small snippets of code, and don't always explicitely show you
what namespace the commands/funcctions/objects belong to.

In VS.net, you can start typing a namespace and it will show you it's
children automatically. I was wondering how one goes about doing the
reverse...picking a object and figuring out what it's parent namespace is.
Is there a way to search MSDN for that information? Sometimes, MSDN
information is arranged by namespace, so I can just follow the hierarchy up,
but, for example, in this case with Cache, I was completely lost as to how
to figure out what namespace I needed.

-Darrel
 
No, there is no "reverse lookup". You will just need to become familiar
with the .NET namespaces and the classes within. Visual Studio Whidbey
(.net 2.0) will have this feature. When you declare a type and the
namespace isn't included, there will be an option to declare the namespace
at the top, but for now, there is nothing.

bill
 
No, there is no "reverse lookup". You will just need to become familiar
with the .NET namespaces and the classes within. Visual Studio Whidbey
(.net 2.0) will have this feature.

Damn. Yet another reason for me to get more anxious waiting for that update
;o)

Thanks, bill!

-Darrel
 
Back
Top