按角色自定义 Sitecore 缓存

Custom Sitecore cache by role

我正在尝试找出按 Sitecore 角色缓存 html 的最佳方法。我正在考虑使用 VaryByParam 但我没有静态绑定我的渲染。它们都是动态添加到页面中的。我正在使用 Web 表单,如有任何帮助,我们将不胜感激

Sitecore 的 "Vary By Parm" html 缓存用于呈现参数。根据您的代码依赖性,您选择正确的 varBy 缓存参数

参见:

creating-sitecore-sublayouts-dynamically

basics-of-html-caching

有时,如果默认的 HTML 缓存与您的逻辑不匹配,您可以使用自定义 Sitecore 缓存来重载或创建自己的 "var By" 请参阅 Sitecore Custom Cache

所以我最终使用了您的部分解决方案,一月。我添加了一个答案,因为它不像启用 Vary By Param 那么简单。

首先我必须实现 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 "";

    }
}

然后我不得不制作一个继承自 Sitecore.Web.UI.WebControls.Sublayout 的子布局来替换 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;
    }

现在剩下要做的就是添加一个子布局渲染来替换管道渲染 calls.This class 继承自 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;
    }

现在所有的代码都完成了,只需要添加到 web.config 修改的行是

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

现在

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

编辑: 为此,您需要在 sitecore

中启用 VeryByParam

这篇文章对我帮助很大 http://sitecoreblog.alexshyba.com/sitecore_output_caching_kick_it_up_a_notch/