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