[英]Custom Sitecore cache by role
我正在嘗試找出通過Sitecore角色緩存html的最佳方法。 我在考慮使用VaryByParam,但我不會靜態綁定渲染。 它們都被動態添加到頁面中。 我正在使用Web表單,將不勝感激
因此,我確實在一月份使用了您的解決方案的一部分。我添加了一個答案,因為它並不像僅啟用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;
}
現在剩下要做的就是添加一個子布局渲染以替換管道調用的渲染。該類繼承自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/
Sitecore html緩存的“ Vary By Parm”用於呈現參數。 根據您的代碼依賴性,選擇正確的varBy緩存參數
看到:
有時,如果默認的HTML緩存與您的邏輯不匹配,則可以使用自定義Sitecore緩存處理大量內容,或者創建自己的“ var By”(請參閱Sitecore定制緩存)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.