How to cache differrent versions of page and then call specific version from http request

M

moondaddy

I have a products catalogue that I'm putting online and there will be
between 20 to 50 different pages of products. Each page contains a datagrid
of products for a given category. However, the categories change from time
to time so the list of categories must be dynamic and database driven (as is
the menu used to select the category to load in the products page). The way
I see it there's 2 possible options. dynamically create each aspx page when
its not found in the system cache (for example if category 22 was selected
from the navigation menu then build a page called Cat_22.aspx, cache it and
then return it to the client), or build one page with one datagrid and cache
each different instance of it (one instance per category as they are
requested). If the 2nd option is the way to go, then I have 2 pointed
questions: 1) How do I cache different versions of this page and 2) How
do I call a specific version from the requesting browser?

Lastly, if it makes any difference, I'm using VS2003 and doing all of this
in a frameset where the nav menu is a web user control in the left-hand
frame called contents and the products are being displayed in the right hand
frame called main. So far I've been able to wire things up OK where the
menu can pass a category ID to a hidden control on the products page and
then call a button click event on the products page to do a postback and
return a datagrid with the products for that category. But in the future I
think this must be done differently since when a user selects a category
from the nav menu, an html page could be in the main frame such as the home
page or about us page. Therefore I would have to pass a url in the target
frame like:
Products.aspx?CategID=22 or something like that (although I've never used
this technique before).

Can someone please advise? Thanks.
 
M

Michael Mayer [C# MVP]

I would do one page with the query string, as you mentioned
(Products.aspx?CategID=22 )
Then use output caching to have it cached on the server side (saves the time
to dynamically receate the page). The following caches for an hour (may be
an eternity) but every CategID gets its own version:

Put a line like this at the top of the aspx page:

<%@ OutputCache Duration="3600" VaryByParam="CategID" %>

Parsing the Query string is easy: (here in vb.net):

Dim CategID As String = Request.QueryString("CategID")

Hope that helps you get started...
 
S

Steven Cheng[MSFT]

Hi moondaddy,


Thanks for posting in the community!
From your description, you had a ASP.NET web page to retrieve certain
records from database via a certain key. And the key is passed by a
querystring in the url such as
"http://server/app/getdatapage.aspx?keystring= ....". For performance
issues, you'd like to cache the certain page's response via the keystring's
value so that when the certain page is requested via the same keystring, a
cached response will be output, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I quite agree to Michael's suggestion that use the
Page's "OutputCache" attributes to specify caching a page's response output
stream. And the "VaryByParam" is used when specify the querystring param
depend on which the page will determine whether to generate a new version
or use the cached output response stream. For example:
<%@ OutputCache duration="60" varybyparam="CategID" %>
That instruct the page to determined whether to first retrieve page in
cache or generate new version according to the "CategID" param.

For more detailed info on using param caching for page's output, you may
view the following reference in MSDN:
#Caching Versions of a Page, Based on Parameters
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcachingversionsofp
agebasedonparameters.asp?frame=true

Also, if you'd like more customized caching control, the ASP.NET also
provide custom output caching control via the
"VaryByCustom" attribute, and here is the tech reference on using this:

#Caching Versions of a Page, Based on Custom Strings
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcachingversionsofp
agebasedoncustomstrings.asp?frame=true

Please check out the above items to see whether they're helpful. If you
need any further assistance, please feel free to let me know.


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.)
 
M

moondaddy

Thanks Steven & Mike! That works excellent. Now how about taking it one
step further and cache each version of the page on the client browser? Is
this possible and if so, how would I approach it?
 
S

Steven Cheng[MSFT]

Hi moondaddy,


Thanks for your response. I'm glad the preceding suggestions have worked
for you. As for the new question on "caching the multi-version of pages on
client side" you mentioned in the reply, I think the ASP.NET's page cache
mechanism doesn't provide such functions. The ASP.NET page's output caches
are all stored on the serverside's caching memory. As for the page's cache
on client side's browser, this is the default behavior, every response to
client brower will has a cache at the client unless you manually disabled
it, that's why if you view a page and then navigator to another page and
use the "back" button on the browser to turn back to the former page, we
can still see the former version of the former one, yes? However, if we
request the page again(use the "refresh" button), the browser will request
a new version of the certain page rather than use the client side's cache.
So I'm afraid the multi-verion output cache is limited to the serverside ,
do you think so?
If you have any questions, please feel free to let me know.



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.)
 
E

Eric Lawrence [MSFT]

You can flag a response to be stored on the client by using the Expires and
Cache-Control headers.

Response.Cache.SetExpires(dtExpires);
Response.Cache.SetMaxAge(dtExpires - dtNow);

Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

moondaddy

This sounds interesting but I don't fully understand (still green at web
dev.). Can you please elaborate a bit by telling me how/where to use this
line of code? Thanks in advance!
 
S

Shahar

moondaddy said:
This sounds interesting but I don't fully understand (still green at web
dev.). Can you please elaborate a bit by telling me how/where to use this
line of code? Thanks in advance!


put in the page load event and make sure that the page not postback i.e
void Page_Load()
{
if (!this.isPostBack)
{
// cache...
}
}
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top