473,721 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Output caching dynamic pages

Hi.

I had a very frustrating afternoon and evening but I have got it all under
control now so all of a sudden I am in a good mood. I want to share some
insights on output caching with you lot.

After looking at the use of the OutputCache directive and tinkering with it
a bit I found its usability.very limited. Think of it: it is okay for static
content. Well that's nice but no big deal. Static content is cheap anyway,
the file system cache will take care of caching static content for me
automatically, ASP.NET doing it once again seems more like a waste to me
than an efficiency boost.

So much for caching static content. For dynamic content it is absolutely
useless because there is no way to invalidate the cache based on a parameter
value which is the one thing you want when applying output caching on a
dynamic page. Take a message board page. When someone posts a message, you
would do something like

<form id="MsgForm" action="board.a spx?action=post " method="Post">

so you can detect the post-back based on the value of the action parameter.
You would switch to code that saves the message and then redirect to display
the updated message list. Of course, at that time you would want the cache
to be invalidated in order not to have the old message list being returned
instead of your save logic being invoked. Currently there is no way to deal
with this using the OutputCache directive. All you can do with VaryByParam
is add more cached pages, you cannot exclude a page based on a parameter
value which is what you need here.

It doesn't seem hard for the ASP.NET developers to expand the OutputCache
directive to support this kind of behavior and I don't see why they did not
do so in the first place. Code that invalidates a page isn't of much use
while ASP.NET keeps serving cached responses, disregarding any server side
logic. It's catch 22, you cannot pull yourself from the swamp pulling your
bootstraps.
So, this morning I hit something interesting:
System.Web.Http CachePolicy.Add ValidationCallb ack. At last, this seemed like
what I had been looking for all along: a call-back. I would be asked whether
it was okay to use the cached page or not. How convenient, I, the
programmer, actually got a say in the way requests were handled again. Nice!

I had three kinds of request to deal with for the same page. First there was
the parameterless request, listing all previously posted data. Then there
was the edit request for which the response is a form to be filled in and
posted back by the user. And finally there was the post request for which
the posted form is received , the data collected and stored and a redirect
is performed to simulate the first type of request.

What took me about 4 hours to figure out is that for the post request my
call-back never gets called. This makes sense, the logic behind my post
request does not produce a response, it just stores data and that's it. If
there's no response, there will be no cached data for the request hence
there is nothing to query for ASP.NET.

At this point it may seem simple. Why bother with call-back routines if we
do get the post request at all times, just when we need to take control? The
problem is that you don't have the context for normel (parameterless)
request at that time so you cannot invalidate it when you want to. Perhaps
there is a way I am not aware of to get to a page's cache from within
another context after al. If so I would like to hear about it. I found
another solution.

The way to make it work just perfectly is to declare a static flag in your
page class, have that set by your store logic (that gets invoked by the post
request) and then check that flag in the call-back implementation for a
normal request. If the flag is set it means we had a new post and the cached
list is not current anymore so we need to invalidate. You should also reset
the flag.
The aspx file could have an ordinairy OutputCache directive:

<%@ OutputCache Duration="16934 400" VaryByParam="*" Location="Serve r" %>
<!-- keeping page hot for 4 weeks -->

and on any normal request expiration could be refreshed to the same same
amount ().

Below are the interesting parts of the code-behind file.
namespace mwte
{
using System;
using System.Web;

public class MyPage : System.Web.UI.P age
{
public string URL_of_this_pag e = null;

static Boolean IsDirty = false;

static protected void Validate(HttpCo ntext context, Object data, ref
HttpValidationS tatus status)
{
string action = (context.Reques t["action"] == null) ? "" :
context.Request["action"];

// The default page should only expire when IsDirty
// was set because of an earlier post request.
if (action == "")
{
if (IsDirty)
{
status = HttpValidationS tatus.Invalid;
// reset flag
IsDirty = false;
}
else
{
status = HttpValidationS tatus.Valid;
// reset expiration time to a day from this moment on
context.Respons e.Cache.SetExpi res(DateTime.No w.AddDays(1));
}
return;
}

// the edit form never expires
if (action == "edit")
{
status = HttpValidationS tatus.Valid;
return;
}

// Note that we do not test for action == "post" here. While it would
// be nice if we could, we can't because Validate is not called for
// post requests. There is no response for post requests, hence no
// cached page, hence Validate is not called. We need to set IsDirty
// in Page_Load instead.
}

protected void Page_Init(objec t sender, EventArgs e)
{
URL_of_this_pag e = Request.ServerV ariables["SCRIPT_NAM E"];
Response.Cache. AddValidationCa llback(new
HttpCacheValida teHandler(Valid ate), null);
}

protected void Page_Load(objec t sender, EventArgs e)
{
// no local caching
Response.Expire s = 0;

string action = (Request["action"] == null) ? "" : Request["action"];

if (action == "")
{
// keep page hot another 4 weeks
Response.Cache. SetExpires(Date Time.Now.AddDay s(28));

// aspx with asp:Xml control does it all
return;
}

if (action == "edit")
{
// let aspx respond with input form
return;
}

if (action == "post")
{
// store logic
// ...

// signal next request handler to invalidate the cached page
IsDirty = true;

// display (and cache) the new list
Response.Redire ct(URL_of_this_ page);
return;
}
}
}
}


Nov 18 '05 #1
0 2133

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
9499
by: Behzad | last post by:
Hi all I'am ASP programmer and I have built a site that users can upload and download files.All things store in a DB and everytime someone enters a page,the application requery the Db and shows him data. but there is aproblem: all my pages caches before Client and i can not see fresh page.for example if I did some activity on Monday on Wed. i Still see the old page that was produced on Monday.I have put some header in BOth Html And ASP...
0
1737
by: Troy Simpson | last post by:
Hi, I have a website which is made up of dynamic pages. Each page that's loaded has some code which looks at which template to load amongst other things, which causes the page to take a little while to be processed. At the moment I have programmatic page caching in the "loader" using paramater caching to send a cached version of every requested page back to the user. How can I get particular elements inside each page to cache without...
8
2125
by: Nick Gilbert | last post by:
Hi, Our development web server (Win 2003 Server) seems to be caching the output of various files (CSS and .aspx files) if they have no query string. The moment you put a question mark in the URL, it's fine. The problem is that if you modify a file, it still shows you the old file unless you restart the website or IIS itself. It's definitely not the browser that's caching the files - IIS is actually serving the wrong file.
13
1650
by: tshad | last post by:
How do we handle Caching in IE? It seems that my system works fine in Mozilla and Netscape when I make changes. But in IE the changes are not brought across when someone has been to the site before. If they have been here before and hit the same page, the old page is rendered which will cause errors as things that are expected with the new changes are not being done, such as session cookies being created or updated.
1
1342
by: Ben Fidge | last post by:
What are best practices for page output caching on pages that are dynamically generated from database tables. Our site has left-hand navigation that is comprised of dynamically generated menus of product categories. The categories are taken from several database tables which rarely change. This left-hand navigation is included on every single page in the site and as such is wrapped up in a user-control. However, when I enabled caching...
3
6457
by: ary | last post by:
I try to create a weblog host site! in this case i can't use cache for every page because that cause to be my Server ram full of caching page. but if I can save cache in hard disk my problem solved? can help me? thanks.
3
1755
by: Smithers | last post by:
Just wondering what it would take to cache a copy of the output HTML from a dynamically constructed aspx page before it is sent to the browser. Reason being: the page is constructed of a few user controls, each of which queries a SQL Server database for their content. The content is not likely to change frequently at all - so I'd like a way to cache the final page upon first request. Then subsequent requests are served from the cache -...
0
1384
by: Jordan S. | last post by:
Using .NET 3.5... in a "plain old" .aspx page I have the following code in the Init event: this.Context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); this.Context.Response.Cache.SetCacheability(HttpCacheability.Public); this.Context.Response.Cache.SetValidUntilExpires(false); It works great. Testing shows that the page is cached for 60 seconds as expected. Specifically, I insert the current date/time into the Response...
2
1835
by: Ken Fine | last post by:
I have a question about ASP.NET output caching. I want to use screen scraping as a temporary hack to pull in some complex Classic ASP-rendered content into some ASP.NET pages: protected String ReadHtmlPage(string url) { WebResponse objResponse; WebRequest objRequest = System.Net.HttpWebRequest.Create(url); objResponse = objRequest.GetResponse();
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.