Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • scottwilson 5 posts 28 karma points
    Oct 19, 2010 @ 17:05
    scottwilson
    1

    Output Caching in 4.5

    I noticed that the outputcache 4 Umbraco wasn't running in 4.5. I wanted to share my solution. I could make a module for it to make it even easier, but it's really a simple fix.

    First, create a MasterPage class as follows:

    public class CachedUmbracoTemplate : MasterPage
    {
        public CachedUmbracoTemplate()
        {
            this.Load += new EventHandler(CachedUmbracoTemplate_Load);
        }

        void CachedUmbracoTemplate_Load(object sender, EventArgs e)
        {
            HttpCachePolicy policy = this.Response.Cache;
            policy.SetCacheability(HttpCacheability.Server);
            policy.SetExpires(this.Context.Timestamp.AddSeconds((double)3600));
            policy.SetMaxAge(new TimeSpan(0, 0, 3600));
            policy.SetValidUntilExpires(true);
            policy.SetLastModified(this.Context.Timestamp);
            policy.VaryByParams.IgnoreParams = false;
            policy.VaryByParams["*"] = true;

            Response.AddFileDependency(MapPath("~/App_Data/umbraco.config"));
        }
    }

    Then, in any templates for document types that need output caching, just make the masterpage subclass your new CachedUmbracoTemplate:

    <%@ Master Language="C#" 
        MasterPageFile="~/umbraco/masterpages/default.master"
        Inherits="SaraLee.UmbracoExtensions.Web.UI.Masterpages.CachedUmbracoTemplate" %>

     

    Et Voila.

     

  • scottwilson 5 posts 28 karma points
    Oct 20, 2010 @ 03:07
    scottwilson
    2

    Looks like the AddFileDependency doesn't work when you add from within the masterpage. It's probably not exactly what you want anyway. Here's revision 1.1 that uses the AfterPublish event to signal to ASP.NET that all cached pages are invalid. I am invalidating all files when any one is published as we are reusing a lot of content - pulling touts in from one page to another. Log4Net is optional, of course, but is handy since it shows the caching in action.

    public class CachedUmbracoTemplate : MasterPage
    {
    private static readonly ILog log = LogManager.GetLogger(typeof(CachedUmbracoTemplate));
    private bool isCacheValid = true;
    private string cachedUrl;

    public CachedUmbracoTemplate()
    {
    this.Load += new EventHandler(CachedUmbracoTemplate_Load);

    Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
    }

    void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
    {
    if (log.IsDebugEnabled) log.DebugFormat("Document '{0}' published. Setting invalid cache for '{1}'.", sender.Id, cachedUrl);

    /* Because we're using touts for content reuse, I can't be too picky about what I invalidate */
    this.isCacheValid = false;
    }

    void CachedUmbracoTemplate_Load(object sender, EventArgs e)
    {
    HttpCachePolicy policy = this.Response.Cache;
    policy.SetCacheability(HttpCacheability.Server);
    policy.SetExpires(this.Context.Timestamp.AddSeconds((double)3600));
    policy.SetMaxAge(new TimeSpan(0, 0, 3600));
    policy.SetValidUntilExpires(true);
    policy.SetLastModified(this.Context.Timestamp);
    policy.VaryByParams.IgnoreParams = false;
    policy.VaryByParams["*"] = true;

    this.Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(HttpCacheValidationCallback), null);
    cachedUrl = Request.Url.ToString();
    }

    void HttpCacheValidationCallback(HttpContext context, Object sender, ref HttpValidationStatus status)
    {
    if (log.IsDebugEnabled) log.DebugFormat("Page '{0}' requested. Cache validity: {1}", context.Request.Url.ToString(), isCacheValid);
    status = isCacheValid ? HttpValidationStatus.Valid : HttpValidationStatus.Invalid;
    }
    }
Please Sign in or register to post replies

Write your reply to:

Draft