6
votes

With Sitecore 7, most of my query strings have become custom routes. For example:

OLD: /calendar?year=2013&month=7&day=14
NEW: /calendar/2013/7/14

As a result, I find myself in need of varying the HTML cache of my renderings by URL, rather than by query string. Does Sitecore provide a convenient way of varying cache by URL for MVC renderings?

UPDATE

I have found that all MVC renderings pass through the mvc.renderRendering pipeline, where their cache key is generated in the following class:

Sitecore.Mvc.Pipelines.Response.RenderRendering.GenerateCacheKey

By overriding the GenerateKey(Rendering rendering, RenderRenderingArgs args) method of this class, I am able to successfully append the raw URL using site.Request.FilePath. The problem I face now is how do I extract this cache setting from the rendering itself? I think I need to create a new "VaryByUrl" caching option on the rendering, but i'm not quite sure.

UPDATE 2

I was able to read a custom field off of the rendering definition item using the following code. It works, but it only looks at the definition item (not the actual instance of the rendering in the presentation details)

protected override string GenerateKey(Rendering rendering, RenderRenderingArgs args)
{
    var key = base.GenerateKey(rendering, args);

    if (rendering.RenderingItem.InnerItem.Fields["VaryByUrl"] != null)
    {
        var varyByUrl = ((CheckboxField)rendering.RenderingItem.InnerItem.Fields["VaryByUrl"]).Checked;
        if (varyByUrl) key += GetUrlPart(rendering);
    }

    return key;
}
1

1 Answers

1
votes

We also had a need for VaryByUrl but in a WebForms architecture (v.6.5.0).

For the sublayout definition, I overrode the Sublayout class to add VaryByUrl similar to how you accomplished it above.

Assuming your Sitecore version still uses the same Rendering Parameters templates, for an instance of the rendering, you have to create your own version of the class referenced in the Source field of /sitecore/templates/System/Layout/Rendering Parameters/Standard Rendering Parameters/Caching/Caching.

This is the class reference in the Source field: type=Sitecore.Shell.Applications.Layouts.DeviceEditor.CachingField,Sitecore.Client