The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
Configuring output cache is very important part of the performance tuning process of your website. Sitefinity offers an easy way to configure output cache and there is a documentation article available providing detailed information about this.
When you request a Sitefinity page our PageRouteHandler class checks whether there is such a cached version of the page and serves it. If the page is not present in cache we will add it and serve it from there on the next request. By default when you try to access a page of your website using different browsers, separate output cache will be created for every browser. To recognize which browser currently requesting particular page PageRouteHandler relies on browser’s user-agent variable.
In this blog post I will demonstrate how you can change the default behavior and implement a custom page route handler that will create non-browser specific cache. To achieve this we need to create a new route handler that will inherit from PageRouteHandler. The method responsible for managing output cache is ApplyServerCache and we will need to override it in order to change its default behavior. This behavior can be modified by changing the value of UserAgent property.
As I already mentioned above we need to create a new class that will inherit from PageRouteHandler and then override ApplyServerCache method.
using
System;
using
System.Web;
using
Telerik.Microsoft.Practices.Unity;
using
Telerik.Sitefinity.Abstractions;
using
Telerik.Sitefinity.Web;
namespace
SitefinityWebApp
{
public
class
CustomPageRouteHandler : PageRouteHandler
{
protected
override
bool
ApplyServerCache(HttpContextBase context, Telerik.Sitefinity.Modules.Pages.Configuration.OutputCacheProfileElement profile, PageSiteNode siteNode)
{
int
duration = profile.Duration;
bool
slide = profile.SlidingExpiration;
var cache = context.Response.Cache;
cache.SetCacheability(HttpCacheability.Server);
cache.SetExpires(DateTime.Now.AddSeconds(duration));
cache.SetMaxAge(
new
TimeSpan(0, 0, duration));
cache.SetValidUntilExpires(
true
);
if
(slide)
{
cache.SetSlidingExpiration(
true
);
cache.SetETagFromFileDependencies();
}
cache.VaryByHeaders.UserAgent =
false
;
cache.VaryByParams.IgnoreParams =
false
;
cache.VaryByParams[
"*"
] =
true
;
cache.VaryByHeaders[
"host"
] =
true
;
return
true
;
}
public
static
void
RegisterType()
{
ObjectFactory.Container.RegisterType(
typeof
(PageRouteHandler),
typeof
(CustomPageRouteHandler));
}
}
}
protected
void
Application_Start(
object
sender, EventArgs e)
{
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
}
void
Bootstrapper_Bootstrapped(
object
sender, EventArgs e)
{
ObjectFactory.Container.RegisterType(
typeof
(PageRouteHandler),
typeof
(CustomPageRouteHandler));
}
I have prepared and a short video illustrating how our custom page route handler is working.
You can download the complete sample from here.
View all posts from Kaloyan Savov on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.