Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / December 2004

Tip: Looking for answers? Try searching our database.

Caching Advice Required

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
greg - 30 Nov 2004 23:47 GMT
I have an ASP app that i'm porting over ASP.Net

In an asp page i have many server.execute statements.

So in the asp page code i have something like this

<%
session("objectid") = 1001778
server.execute("multimedia.aspx")

session("objectid") = 1001779
server.execute("multimedia.aspx")

session("objectid") = 1001899
server.execute("listrecords.aspx")

session("objectid") = 1001778
server.execute("multimedia.aspx")

%>

Now i want only some code on the multimedia.aspx page to be cached as some
of the queries on the page are dynamic.
However i also want the page caching to vary based upon the objectid number
in the session variable. (BTW i am using a method that allows ASP sessions
to be shared with ASP.net sessions).

what i don't quite understand is that if i set the OUTPUTCACHE at the top of
page (in the .aspx file) to be cached and i use user controls in the ASPX
page for the dynamic queries is it possible to set the user controls so they
are not cached or ....
can you only do it the other way round and have the cached portion in user
controls.

An example of the multimedia.aspx file logic may read something like this:

run query to get parameters for this multimedia object using the session
objectid variable as a parameter
output html (ie table with colours) based upon this query (this is what i
want to cache)

then run query to get a list of .gifs from the database (they can change
regularly so i want this dynamic)
output html (not cached)

--------------------------------------------------------------------------------
The same multimedia.aspx can appear twice (with the same session objectid as
a parameter) on the same ASP page. In each multimedia.aspx page that gets
passed a different session objectid the html output varys slightly (ie table
colours etc.)

Thanks.
Ben Strackany - 01 Dec 2004 00:32 GMT
One easy way to handle all that would be to use data caching. Create a
"multimedia" user control that has an ObjectID property. The user control's
code takes that objectid, calls the database, gets a dataset back, builds
some HTML, & returns it. Your user control could cache the dataset e.g.

  Cache.Insert("dataset"+this.ObjectID,dsTheResults)

Or it could build the HTML from the dataset & just cache the resulting HTML

  Cache.Insert("html"+this.ObjectID,strSomeHTML)

The code would do the normal stuff of checking the cache for null,
repopulating it as needed (maybe with delgates), etc.

If you were set on using outputcaching you'd put the outputcache into the
multimedia control, not the ASPX, & use one of the varyby* attributes. But I
think that'll prove more trouble than it's worth in this situation.

Signature

Ben Strackany
www.developmentnow.com

<a href="http://www.developmentnow.com">dn</a>

> I have an ASP app that i'm porting over ASP.Net
>
[quoted text clipped - 48 lines]
>
> Thanks.
greg - 01 Dec 2004 04:27 GMT
Thanks,
That sounds good but what i don't quite understand is how i cache around the
dynamic stuff.
For example in the multimedia.aspx

I run a query based upon parameter (session variable) then i output html
which you say i can cache.
Problem is that the dynamic queries appear in the page logic inbetween the
html i want to cache.

So i run the first query
then i might say something like this.
<%
if object.recordcount=1 then
 %><%=html%><%
     // run dynamic query and output some dynamic html
 %><%=morehtml%><%

else

 %><%=html%><%
    // run dynamic query here and output some dynamic html
 %><%=morehtml%><%

end if
%>

how do i easily cache the html around the dynamic queries?
With your method wouldn't i require lots of cache inserts for each block of
html?

So ideally i would like to cache the aspx page but run some usercontrols
(with no caching) to output the dynamic stuff
I'm not sure if this is possible however.

> One easy way to handle all that would be to use data caching. Create a
> "multimedia" user control that has an ObjectID property. The user
[quoted text clipped - 79 lines]
>>
>> Thanks.
Ben Strackany - 01 Dec 2004 19:48 GMT
Yeah, anything you cache will cache the stuff inside it. So you're better
off leaving the main ASPX uncached.

---aspx (not cached)
   -- some queries
   -- multimedia control (cached)
   -- some queries
   -- multimedia control (cached)
   -- some queries
   -- listrecords control (not cached)
   -- some queries
   -- multimedia control (cached)
Signature


After looking at your code again, I realize you don't have to use user
controls, since you're doing server.execute. Which if it works for you, is
probably ok (although user controls may perform better under load).

If you're just concerned about performance, you could change multimedia.aspx
to cache the database results, & leave the HTML generation alone. So in
multimedia.aspx whenever you called the database you could first see if the
dataset was in the cache. If it was't, you'd call the database, get back the
dataset, store it in the cache w/ a key associated with the dataset (e.g.
the objectid), and then use the dataset to draw html, whatever. Kinda like
this:

<%
string strObjectID = Session("ObjectID")
DataSet dsObject = Cache.Get("firstdataset"+strObjectID)
if (dsObject==null) {
   // not in cache
   // query the database, get back ds & store back in cache
   Cache.Insert("firstdataset"+strObjectID, dsObject);
}
// use ds to generate some html, whatever
if (dsObject.RecordCount==1) {
   DataSet dsTmp = Cache.Get("seconddataset"+strObjectID)
   if (dsTmp==null) {
   // not in cache
   // query the database, get back ds & store back in cache
   Cache.Insert("seconddataset"+strObjectID, dsTmp);
   // draw some html
}
else {
   DataSet dsTmp = Cache.Get("thirddataset"+strObjectID)
   if (dsTmp==null) {
   // not in cache
   // query the database, get back ds & store back in cache
   Cache.Insert("thirddataset"+strObjectID, dsTmp);
   // draw some html
}
%>

That would be easier. If you really want to cache the HTML also, you would
need to store the HTML into a big string, & cache that string. Like

<%
string strObjectID = Session("ObjectID")
string strHTML = Cache.Get("html" + strObjectID)
if (strHTML==null) {
   // not cached, need to build the HTML
   strHTML = ""
   strHTML += "<b>some html</b>"
   // call the database
   foreach(datarowview drv in DataSet.Tables[0].DefaultView) {
       strHTML += "some more html" + drv["somefield"]
   }
      // cache html
   Cache.Insert("html" + strObjectID, strHTML)
}
// output html
Response.Write(strHTML)
%>

But I don't think it's worth it...I'd suggest caching the database results
first, see how that works out.

FYI, using outputcache works easily when you have values in the querystring
you can use to distinguish between versions (e.g.
"multimedia.aspx?ObjectID=6561526"). Or if you're using user controls, you
can stick outputcache inside them & have them differentiate on querystring,
browser type, ID (i.e. the ID of the user control). There is also
VaryByCustom where you can do some custom coding for caching, but it may not
work out in your situation.

Good luck, lmk how it works out or if you have further questions. The
ASP.NET QuickStart has some good, easy caching examples. Or google. ;)

--
Ben Strackany
www.developmentnow.com

<a href="http://www.developmentnow.com">dn</a>

> Thanks,
> That sounds good but what i don't quite understand is how i cache around the
[quoted text clipped - 114 lines]
> >>
> >> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.