2017-12-18 117 views
0

我有一個自定義產品類型,顯示在自定義列表Web部件中。由於性能方面的原因,我試圖緩存項目,但檢查用戶權限也很重要,因爲並非所有用戶都能看到所有產品。加載Kentico 10網絡節點的最佳方法,包括緩存和權限檢查

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的一部分 - 但我不知道如何將其應用於緩存的產品列表或單個節點上。

那麼有沒有一種很好的方法來實現我想要的?無需遍歷每個節點並單獨檢查用戶是否有權訪問該節點?

回答

1

您錯過了代碼中的緩存部分,我並不確定您的緩存依賴關係。

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應該是「custom_products」+ UserID.ToString()或類似的東西。

+0

我想知道我是否可以緩存產品列表 - 然後再輕鬆/有效地檢查它們的權限。我試圖避免每個用戶緩存它們 - 因爲這看起來不太正確。如果我確實需要查看用戶過濾的一組產品列表,那麼我無法使用緩存也不太合適。希望更有意義。 – Jen

+0

我添加了部分代碼,實際上緩存了我的問題 - 感謝您的建議。 – Jen

+0

最終包含一個用戶標識符作爲緩存鍵的一部分 - 併爲每個用戶進行緩存。儘管它看起來有點奇怪 - 但Kentico指出,即使產品被緩存,它仍然會觸及數據庫來檢查權限! – Jen