Fragmented Cache

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
 
C

Cowboy

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.Public);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.VaryByParams["Category"] = true;

if (Response.Cache.VaryByParams["Category"])
{
//...
}


A couple of links
http://authors.aspalliance.com/aspxtreme/sys/Web/HttpResponseClassCache.aspx
- overview
http://support.microsoft.com/default.aspx?scid=kb;en-us;323290 - more detail
instructions

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

************************************************
Think Outside the Box!
************************************************
 
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 said:
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.Public);
Response.Cache.SetValidUntilExpires(false);
Response.Cache.VaryByParams["Category"] = true;

if (Response.Cache.VaryByParams["Category"])
{
//...
}


A couple of links
http://authors.aspalliance.com/aspxtreme/sys/Web/HttpResponseClassCache.aspx
- overview
http://support.microsoft.com/default.aspx?scid=kb;en-us;323290 - more detail
instructions

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

************************************************
Think Outside the Box!
************************************************
A.M said:
Hi,

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

Any help would be apprecited,
Alan
 
S

Steven Cheng[MSFT]

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.HttpCacheability.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
 
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
 
S

Steven Cheng[MSFT]

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

Steven Cheng[MSFT]

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
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top