By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Fragmented Cache

P: n/a
A.M
Hi,

How can I change the duration of user control's cache inside asp.net's C#
code?

Any help would be apprecited,
Alan
Nov 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
NOTE: I am going to hit the entire topic, so you can see both ways. What you
are asking is the second way.

You can do this declaratively, which is, by far the easiest:

<%@ OutputCache Duration="60" VaryByParam="None" %>

The VaryByParam is most easily implemented by adding properties to the
CodeBehind of your user control. To set parameters in CodeBehind of your
ASPX page, you will have to reference the control. For example, you drag a
control called TopMenu on your ASPX page creating topMenu1

protected TopMenu topMenu1;

You can then set properties in CodeBehind. For example, you have a UserName
property:

topMenu1.UserName = User.Identity.Name;

To set cache programatically use the HttpResponse.Cache Property. This is
from the help file:

Response.Cache.SetExpires(DateTime.Now.AddSeconds( 60));
Response.Cache.SetCacheability(HttpCacheability.Pu blic);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.VaryByParams["Category"] = true;

if (Response.Cache.VaryByParams["Category"])
{
//...
}
A couple of links
http://authors.aspalliance.com/aspxt...lassCache.aspx
- overview
http://support.microsoft.com/default...b;en-us;323290 - more detail
instructions

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"A.M" <IH*******@sapm123.com> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
Hi,

How can I change the duration of user control's cache inside asp.net's C#
code?

Any help would be apprecited,
Alan

Nov 18 '05 #2

P: n/a
A.M
Thank you for reply.

I know how to setup user-control's cache parameters during design time.

Response.Cache has nothing to do with user-control caching. Response.Cache
defines how the entire page is being cached at *client browser* 's cache as
opposed to user-control's cach which defines how the user-control's output
is being cached on *web server*'s memory !!

I am looking for some way to change user-control's cache parameters during
design time.

Alan

"Cowboy" <No************@comcast.netNoSpamM> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
NOTE: I am going to hit the entire topic, so you can see both ways. What you are asking is the second way.

You can do this declaratively, which is, by far the easiest:

<%@ OutputCache Duration="60" VaryByParam="None" %>

The VaryByParam is most easily implemented by adding properties to the
CodeBehind of your user control. To set parameters in CodeBehind of your
ASPX page, you will have to reference the control. For example, you drag a
control called TopMenu on your ASPX page creating topMenu1

protected TopMenu topMenu1;

You can then set properties in CodeBehind. For example, you have a UserName property:

topMenu1.UserName = User.Identity.Name;

To set cache programatically use the HttpResponse.Cache Property. This is
from the help file:

Response.Cache.SetExpires(DateTime.Now.AddSeconds( 60));
Response.Cache.SetCacheability(HttpCacheability.Pu blic);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.VaryByParams["Category"] = true;

if (Response.Cache.VaryByParams["Category"])
{
//...
}
A couple of links
http://authors.aspalliance.com/aspxt...lassCache.aspx - overview
http://support.microsoft.com/default...b;en-us;323290 - more detail instructions

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"A.M" <IH*******@sapm123.com> wrote in message
news:ub**************@TK2MSFTNGP09.phx.gbl...
Hi,

How can I change the duration of user control's cache inside asp.net's C# code?

Any help would be apprecited,
Alan


Nov 18 '05 #3

P: n/a
Hi Alan,

As for the UserControl's output Control, based on my research, we can only
set its output cache declartively at desige time either use the <%@
OutputCache Duration="60" VaryByParam="state" %>( in ascx file)
or the following meta attribute
[PartialCaching(10, "state", null, null)]
public class CacheUC : System.Web.UI.UserControl
in UserControl's code behind class, but both of them are set at designtime
rather than runtime.

As for the Response.Cache.XXX methoed by Cowboy, they're also ok for
caching on serverside(not only at clientside) as long as we set the
cacheability as server, for example:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds( 10));
Response.Cache.VaryByParams["state"] = true;
Response.Cache.SetCacheability(System.Web.HttpCach eability.Server);
...
#don't turn on the Page's trace, that'll cause the programmatically cache
not work

But this is only for caching the whole asp.net web page, not for a partion
of page(a usercontrol).
So to cache a UserControl we have to make the outputcache setting at
designtime.
If you still have anything unclear, please feel free to post here. Thanks.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx


Nov 18 '05 #4

P: n/a
A.M

Thank you Steven for help.

Your reply clarified important points for me.

So if I need to change usercontrol's cache at design time, the only option
that I have is opening the ascx file as a text file, change the cache
parameter on top of the page and write it back into the hard drive. I have
to give wite permission to certain folders to ASPNET user. Do you think that
will work?

Thank you,
Alan



"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:wK**************@cpmsftngxa10.phx.gbl...
Hi Alan,

As for the UserControl's output Control, based on my research, we can only
set its output cache declartively at desige time either use the <%@
OutputCache Duration="60" VaryByParam="state" %>( in ascx file)
or the following meta attribute
[PartialCaching(10, "state", null, null)]
public class CacheUC : System.Web.UI.UserControl
in UserControl's code behind class, but both of them are set at designtime rather than runtime.

As for the Response.Cache.XXX methoed by Cowboy, they're also ok for
caching on serverside(not only at clientside) as long as we set the
cacheability as server, for example:

private void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds( 10));
Response.Cache.VaryByParams["state"] = true;
Response.Cache.SetCacheability(System.Web.HttpCach eability.Server);
..
#don't turn on the Page's trace, that'll cause the programmatically cache
not work

But this is only for caching the whole asp.net web page, not for a partion
of page(a usercontrol).
So to cache a UserControl we have to make the outputcache setting at
designtime.
If you still have anything unclear, please feel free to post here. Thanks.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #5

P: n/a
Hi Alan,

Do you mean that you'd like to use the IO api in asp.net web application to
modify the aspx or ascx file 's content so as to modify the @Outputcache
diretive?

If so , I don't think this is good approach and don't recommend you do
that because:
1. This means will need us to grant the file permission to the aspnet
process account. This is not quite reasonable

2. The ASP.NET runtime will monitor the web application's folder , if the
source file is modified , at runtime ,the certain requested web page(if not
the first time be requested) will be recompiled. And there is a limitation
setting in the machine.config determine that when the modify time exceed
the limitation, the whole application(appdomain) will restart. I don't
think this is what you want to see, yes?

Please have a consider at the above things. If you have anything unclear,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #6

P: n/a
A.M
Thank you for suggestion.

I am considering to write a helper executable that application administrator
can run and it applies cache settings(stored in separated config file) to
ascx files. It shouldn't take much time. so web application won't get
restarted.

Do you think this approach works?

Alan
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:uW*************@cpmsftngxa10.phx.gbl...
Hi Alan,

Do you mean that you'd like to use the IO api in asp.net web application to modify the aspx or ascx file 's content so as to modify the @Outputcache
diretive?

If so , I don't think this is good approach and don't recommend you do
that because:
1. This means will need us to grant the file permission to the aspnet
process account. This is not quite reasonable

2. The ASP.NET runtime will monitor the web application's folder , if the
source file is modified , at runtime ,the certain requested web page(if not the first time be requested) will be recompiled. And there is a limitation
setting in the machine.config determine that when the modify time exceed
the limitation, the whole application(appdomain) will restart. I don't
think this is what you want to see, yes?

Please have a consider at the above things. If you have anything unclear,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #7

P: n/a
Hi Alan,

Thanks for the followup. I still think there will occur some unexpected
issues if you manually modify the aspx or ascx source at runtime since the
asp.net runtime will monitor the applications' folders and files and will
restart the applicaiton no matter your modification will take much time or
not. Anyway, this may work if you don't frequently change the ascx file
and I think a simple test will be more helpful. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.