简体   繁体   中英

Custom Sitecore cache by role

I'm trying to figure out the best way to cache html by Sitecore roles. I'm thinking by using the VaryByParam but I don't statically bind my renderings. They are all dynamically added to the page. I'm using Web forms, any help would be appreciated

So I did end up using part of your solution Jan. I'm adding an answer because it wasn't as easy as just enabling Vary By Param.

First I had to implement a RoleManager

public class RoleManager
{
    private User currentUser;
    public string GetReadRole(Item item)
    {
        currentUser = Sitecore.Context.User;
        //int found = 0;
        foreach (Role role in currentUser.Roles)
        {
            return role.LocalName; //return the role they are in
        }
        return "";

    }
}

Then I had to make a sublayout that inherits from Sitecore.Web.UI.WebControls.Sublayout to replace the default sublayout in sitecore.

protected RoleManager roleManager = new RoleManager();
    public override string GetCacheKey()
    {
        Sitecore.Sites.SiteContext site = Sitecore.Context.Site;
        if ((Cacheable && ((site == null) || site.CacheHtml)) && !SkipCaching())
        {
            if (VaryByParm)
            {
                return base.GetCacheKey() + "_#userRole:" + roleManager.GetReadRole(this.GetItem());
            }

            return base.GetCacheKey();
        }

        return string.Empty;
    }

Now all there was left to do was add a sublayout rendering to replace the rendering the pipeline calls.This class inherited from Sitecore.Web.UI.SublayoutRenderingType

public override System.Web.UI.Control GetControl(NameValueCollection parameters, bool assert)
    {
        var sublayout = new RoleSublayout();
        foreach (string key in parameters.Keys)
        {
            ReflectionUtil.SetProperty(sublayout, key, parameters[key]);
        }
        return sublayout;
    }

All the code is now done and just need to add to the web.config The line modified was

<control template="sublayout" type="Sitecore.Web.UI.SublayoutRenderingType, Sitecore.Kernel" propertyMap="Path=path" />

and it's now

<control template="sublayout" type="YOURNAMESPACE.RoleSublayoutRenderingType, DLLNAME" propertyMap="Path=path" />

Edit: For this to work, you need to enable VeryByParam in sitecore

This article helped me tons http://sitecoreblog.alexshyba.com/sitecore_output_caching_kick_it_up_a_notch/

The "Vary By Parm" for Sitecore html cache is for rendering parameters. Depend on your code dependencies you choose the right varBy cache parameter(s)

See:

creating-sitecore-sublayouts-dynamically

basics-of-html-caching

Sometimes, if the default HTML cache is not to match with your logic, you can use custom Sitecore caching for heavy pieces or create your own "var By" See Sitecore Custom Cache

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM