caching user control output, at runtime, how to?

W

Welman Jordan

Hello,

I met with a tough problem when making my pages. Please help. To
make the problem short, i write down what i want here first, and then
explain my situation in details.

How can I acchieve this:

Only one Page_Load, and then the output of the control is cached
within the Cache[], accessible via a programatically generated
cachekey.

The cache duration is adjustable at RUN TIME.
------------------------------------------------------
My situation:

I wanna make a web page which contains several dynamically loaded
user controls. I do so by using a placeholder control and load some
controls into that holder in the Page_Load event.

Approach 1:
I would like to cache some certain HTML output of the dynamically
loaded controls. I know that using the @ OutputCache directive can
help me do so. However, that's too limited, since the caching
duration has been deternmined at design time, NOT AT RUN TIME!
I know that there's a VaryByCustom parameter, which might be of
help. But I even DO NOT want to specify this directive in the code at
all! I hope that I can design to cache or not cache at run time.

Approach 2:
My another approach is to cache the output stream in the Render
event handler. I attempt to use an StringWriter to catch the output.

TextWriter tempWriter = new StringWriter();
base.Render(new HtmlTextWriter(tempWriter));
_cachedOutput = tempWriter.ToString();

Cache.Insert(CacheKey, _cachedOutput, null,
DateTime.Now.AddSeconds(_cacheTime), TimeSpan.Zero);

However, the codes in the Page_Load of the control still get
executed--I heavily query the database. Thus there's little help
to reduce the CPU load of the server, since the database query
routine still runs.

How to take both the advantage of approach 1 that Page_Load only
runs once, and the advantage of approach 2 that the caching time
is adjustable?

W. Jordan
 
S

Scott Allen

Hi Welman:

The Cache property of the Response object is an instance of
HttpCachePolicy . With the methods and properties of the
HttpCachePolicy object you can programmatically control all of the
cache settings at runtime ins tread of design time.

HTH,
 
W

Welman Jordan

Hello Scott,

Thanks for telling me that.
But what I what is the individual control over the cache
for my user controls, not a page.

It seems that the HttpCachePolicy object can not be of
help.

W. Jordan

Scott Allen said:
Hi Welman:

The Cache property of the Response object is an instance of
HttpCachePolicy . With the methods and properties of the
HttpCachePolicy object you can programmatically control all of the
cache settings at runtime ins tread of design time.

HTH,

--
Scott
http://www.OdeToCode.com

Hello,

I met with a tough problem when making my pages. Please help. To
make the problem short, i write down what i want here first, and then
explain my situation in details.

How can I acchieve this:

Only one Page_Load, and then the output of the control is cached
within the Cache[], accessible via a programatically generated
cachekey.

The cache duration is adjustable at RUN TIME.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top