Best approach for loading Kentico 10 web nodes including caching and permission checking

时间:2017-12-18 06:29:54

标签: kentico

I have a custom product type that gets displayed in a custom listing web part. I was trying to cache the items for performance reasons, but it's also important to check user permissions as not all products are visible to all users.

private static IList<TreeNode> GetUniqueProducts(string clientId, string path)
{
      var pages = CacheHelper.Cache(cs => GetProducts(cs, path), new CacheSettings(10, "cus|" + clientId));
      return GetUniqueProductNamesItems(pages);
}

private static IList<TreeNode> GetProducts(CacheSettings cacheSettings, string rootPath)
{
    var pages = DocumentHelper.GetDocuments().Types("CUS.Product")
                .Path(rootPath, PathTypeEnum.Children)
                .Published().CheckPermissions().ToList();

    if (cacheSettings.Cached)
    {
          cacheSettings.CacheDependency = CacheHelper.GetCacheDependency("nodes|custom|cus.product|all");
    }

    return pages;
}

However I realise that this is caching the first user's list of products. When in fact I want to store the full list of products in cache - but then check permissions before they get displayed.

The only way of checking permission seems to be as part of a DocumentQuery as per above - but I don't know how to apply that to a cached list of products - or on an individual node.

So is there a good way to achieve what I want? Without having to loop through each node and individually check user is authorised to access the node ?

1 个答案:

答案 0 :(得分:0)

您缺少代码中的缓存部分,我不确定您的缓存依赖性。

    var pages = CacheHelper.Cache(cs  =>
    {
        var result = CMS.DocumentEngine.DocumentHelper.GetDocuments().Types("CUS.Product").Path(rooPath, CMS.DocumentEngine.PathTypeEnum.Children).Published().ToList();
        if (cs.Cached) { cs.CacheDependency = CacheHelper.GetCacheDependency("cus.product|all"); }
        return result;
    },
    new CacheSettings(CacheHelper.CacheMinutes(CurrentSite.SiteName), "custom_products"));

如果您检查用户的读取权限,则意味着您对每个用户进行缓存。然后你的缓存应该按用户完成,即cachename应该是&#34; custom_products&#34; + UserID.ToString()或类似的东西。