Clunky Cache Code Conundrum?

  • Thread starter Thread starter Jim Owen
  • Start date Start date
J

Jim Owen

I am storing all my application data in the application cache. Anytime I
have a method as part of an asp.net form, I need to access the objects in
the cache. The only way I can think of to do this is to call something like:

MyDataType LocalVar = (MyDataType)Cache["MyData"];

Which works fine, but I'm having to put that code at the top of every method
in my form that uses the cached data. It seems there must be a more elegant
way to do this. I don't think I can put it in a form-level field, because it
doesn't persist across posts, and I can't wrap the code into a static method
somewhere else because as I understand it the application cache can only be
access from forms, and not from .cs files.

Any ideas?
 
I don't see anything inelegant about it.
It's a single line of code. How could you reduce it down to anything
significantly simpler than that?
 
It's only one line of code, but if you want to share it among all of your
forms, why don't you create a Class that inherits from
System.Web.UI.Page, add that to it, and inherit that class for all your
Pages that need it?

HTH,

Kevin Spencer
Microsoft FrontPage MVP
Internet Developer
http://www.takempis.com
Big things are made up of
lots of Little things.
 
Hi Jim,

You need to import the System.Web namespace at the top of your code.

I always check for HttpContext.Current == null because a lot of my class
files are also used in Non- ASP.NET apps, so its more habit than anything
else.

hth,
Dave
www.aspNetEmail.com


----- Original Message -----
From: "Jim Owen" <[email protected]>
Sent: Thursday, July 03, 2003 11:26 AM
Subject: Re: Clunky Cache Code Conundrum?

Thanks for the advice. I tried what you suggested, but I have two questions:

1) When I try this, I get a compile error stating "The name 'Cache' does not
exist in the class or namespace MyNameSpace.MyClass". This is why I assumed
that the Cache could not be accessed from .cs files.
2) Why do I need to test for HttpContext == null? How could it ever be null?

Thanks again!

- Jim Owen
206-501-6936

Jim Owen said:
I am storing all my application data in the application cache. Anytime I
have a method as part of an asp.net form, I need to access the objects in
the cache. The only way I can think of to do this is to call something like:

MyDataType LocalVar = (MyDataType)Cache["MyData"];

Which works fine, but I'm having to put that code at the top of every method
in my form that uses the cached data. It seems there must be a more elegant
way to do this. I don't think I can put it in a form-level field, because it
doesn't persist across posts, and I can't wrap the code into a static method
somewhere else because as I understand it the application cache can only be
access from forms, and not from .cs files.

Any ideas?
 
btw,
make sure you reference the Cache object via "HttpContext.Current.Cache" in
your code, not just "Cache".

hth,
Dave
www.aspNetEmail.com




dave wanta said:
Hi Jim,

You need to import the System.Web namespace at the top of your code.

I always check for HttpContext.Current == null because a lot of my class
files are also used in Non- ASP.NET apps, so its more habit than anything
else.

hth,
Dave
www.aspNetEmail.com


----- Original Message -----
From: "Jim Owen" <[email protected]>
Sent: Thursday, July 03, 2003 11:26 AM
Subject: Re: Clunky Cache Code Conundrum?

Thanks for the advice. I tried what you suggested, but I have two questions:

1) When I try this, I get a compile error stating "The name 'Cache' does not
exist in the class or namespace MyNameSpace.MyClass". This is why I assumed
that the Cache could not be accessed from .cs files.
2) Why do I need to test for HttpContext == null? How could it ever be null?

Thanks again!

- Jim Owen
206-501-6936

Jim Owen said:
I am storing all my application data in the application cache. Anytime I
have a method as part of an asp.net form, I need to access the objects in
the cache. The only way I can think of to do this is to call something like:

MyDataType LocalVar = (MyDataType)Cache["MyData"];

Which works fine, but I'm having to put that code at the top of every method
in my form that uses the cached data. It seems there must be a more elegant
way to do this. I don't think I can put it in a form-level field,
because
it
doesn't persist across posts, and I can't wrap the code into a static method
somewhere else because as I understand it the application cache can only be
access from forms, and not from .cs files.

Any ideas?
 
Back
Top