Clunky Cache Code Conundrum?

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?
 
S

Steve C. Orr, MCSD

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?
 
K

Kevin Spencer

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.
 
D

dave wanta

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?
 
D

dave wanta

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?
 

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

Similar Threads

Help with code 0
mock django cache 0
To Cache or not 1
Cache Authentication vs Forms Authentication - Thoughts? 4
Cache 4
Can't understand code 2
Looking for distributed cache solutions 0
Esp8266 Code problem 0

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top