加载 Kentico 10 网络节点的最佳方法,包括缓存和权限检查

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

我有一个显示在自定义列表 Web 部件中的自定义产品类型。出于性能原因,我试图缓存这些项目,但检查用户权限也很重要,因为并非所有产品都对所有用户可见。

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;
}

但是我意识到这是在缓存第一个用户的产品列表。事实上,我想将完整的产品列表存储在缓存中 - 但在它们显示之前检查权限。

检查权限的唯一方法似乎是作为上述 DocumentQuery 的一部分 - 但我不知道如何将其应用于缓存的产品列表 - 或单个节点。

那么有什么好的方法可以达到我想要的效果吗?无需遍历每个节点并单独检查用户是否有权访问该节点?

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

    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"));

如果您检查用户读取权限,这意味着您有点为每个用户缓存。然后你的缓存应该按用户完成,即缓存名称应该是 "custom_products"+ UserID.ToString() 或类似的东西。