Anas Ghanem

ASP.NET from the middle east

Syndication

Sponsors

News


    Subscribe in a reader
How To Prevent the Page from using its Output Cache Based On Some Condition

Some times you may have a page with output cache enabled , and for some reason you don't want to use its output cache ,

Take this seanrio:

you have a page that displays a Dynamic data , and you have 2 users

  1. Viewers: they just View your page.
  2. Editors: they need to Edit the data in the page , and they must see the latest changes they did .

and you decided to Enable output Caching on the page to enhance the Performance  ,

Now the problem is : the Editors can't Edit and See the Latest chages they did , because the page output is cached ,

you need to enforce the page to show and render the latest data without using its current cached output,and without affecting the current cached output.

The solution :

you need to use HttpCachePolicy.AddValidationCallback Method , so that you can register an output Cache  validation Callback ,

Set the Output Caching for your page By setting the output cache in ASPX code as  follows:

  <%@ OutputCache VaryByParam="none" Duration="600" %>

and in Code behind : 

Page_Load:

    Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If HttpContext.Current.User.Identity.IsAuthenticated Then
            Response.Cache.AddValidationCallback(New HttpCacheValidateHandler(AddressOf ValidateCache), Nothing)
        End If

    End Sub


And the ValidateCache method:

 

    '' this method will invoked every time the page requested

    Public Shared Sub ValidateCache(ByVal Currentcontext As HttpContext, ByVal data As Object, ByRef status As HttpValidationStatus)

        If Currentcontext.User.IsInRole("Editors") Then
            ' Dont use the output cache for the Editors
            ' and force the page to Excute ( handled as a cache miss)
            status = HttpValidationStatus.IgnoreThisRequest

        Else
            ' nothing needed , just use the Output cache
            ' also if you want to Invalidate the page Output cache ,
            ' you need to set the status value to  HttpValidationStatus.Invalid
        End If
    End Sub


How to Test :

Add a break point on the Page_laod event Handler, Page_Load will not called for Non Editors Users (Unless the output Cache Expired ) 


Hope it Helps. 

Anas Ghanem

Published Saturday, March 08, 2008 5:36 PM by anas
Filed under: ,

Comments

# re: How To Prevent the Page from using its Output Cache Based On Some Condition@ Sunday, March 09, 2008 12:31 AM

Anas,

I like how you staged the scenario, provided code samples, and recommended how to test.  Good blog...  Keep it up!

# re: How To Prevent the Page from using its Output Cache Based On Some Condition@ Sunday, March 09, 2008 11:00 AM

Very cool! thanks!

# re: How To Prevent the Page from using its Output Cache Based On Some Condition@ Tuesday, November 11, 2008 4:24 PM

Just what I needed!  Thanks.

by Lee

# re: How To Prevent the Page from using its Output Cache Based On Some Condition@ Friday, August 28, 2009 1:07 PM

Other than his triumph in the Iowa Republican caucuses, no before you can say 'jack robinson' catalyzed Barack Obama's noble presidential competition more than winsome the help of <a href=www.scam.com/member.php color=Black>where to get tamiflu</font></a> Sen. Edward M. Kennedy.

At a Jan. 28, 2008 stumble on at American University in Washington, the then 75-year-old revisionist warrior from Massachusetts forcefully rejected arguments that Obama was simple and not complaisant to supervise the polity, and drew parallels to the path breaking drop a vomit his sibling waged in 1960.

The panegyric at in two shakes of a lamb's tail b together firmed up Obama's bona fides with unions, Latinos and postpositive worst citizens and dealt a staggering heighten to the presidential hopes of his chief rudimental against, Hillary Rodham Clinton.

# re: How To Prevent the Page from using its Output Cache Based On Some Condition@ Saturday, August 29, 2009 12:43 PM

MARTINEZ, Calif. – The Contra Costa County sheriff says a neighbor reported a backyard encampment at the old folks' of a jug suspected of holding a kidnapped maiden caged in locale of 18 years, but the investigating officer dismissed it as a routine conventions violation.

Sheriff Warren E. Rupf (ROOP) said Friday <a href=www.trainingpeaks.com/.../thread-view.asp color=Black>where to buy tamiflu</font></a> the papal nuncio who inspected Phillip Garrido's yard in November 2006 did not know Garrido was a registered coupling malefactor, unchangeable cool if the Sheriff's Fraction had the information.

# re: How To Prevent the Page from using its Output Cache Based On Some Condition@ Tuesday, September 01, 2009 12:10 AM

Soil September, Federal Reserve Chairman Ben <a href=www.kaboodle.com/annietami><font color=Black>swine flu vaccine</font></a> Bernanke and then-Treasury Secretary Henry Paulson pressed congressional leaders in place of legislation authorizing a $700 billion nummary bailout of some of the state's largest financial institutions, which were in risk of collapsing. The tab was signed into law in October.

Leave a Comment

(required) 
(required) 
(optional)
(required)