如何加速Umbraco中的节点迭代?

时间:2012-11-07 17:29:28

标签: .net umbraco

我已经搜索了一个网站,该网站遍历特定文件夹下的所有节点,然后根据特定条件筛选出节点。该网站本身用于搜索公寓。这是我用于构建集合的函数:

private static List<Apartment> GetApartmentsCollection()
        {
            List<Apartment> apartmentsCollection = new List<Apartment>();

            Document apartmentsContainer = new Document(Convert.ToInt32(ConfigurationManager.AppSettings["apartmentsContainerNodeId"]));

            if (apartmentsContainer.HasChildren)
            {
                Document[] buildings = apartmentsContainer.Children;
                foreach (Document building in buildings)
                {
                    if (building.Published)
                    {
                        // Store building id for apartment
                        int buildingId = building.Id;
                        if (building.HasChildren)
                        {
                            Document[] apartments = building.Children;
                            foreach (Document apartment in apartments)
                            {
                                if (apartment.Published)
                                {
                                    apartmentsCollection.Add(new Apartment(apartment.Id,
                                                                           buildingId,
                                                                           Convert.ToInt32(apartment.Text),
                                                                           (int)apartment.getProperty("apartmentType").Value,
                                                                           (int)apartment.getProperty("floor").Value,
                                                                           (int)apartment.getProperty("status").Value,
                                                                           (int)apartment.getProperty("bedrooms").Value,
                                                                           Convert.ToInt32(apartment.getProperty("price").Value),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room1").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room1").Value.ToString()),
                                                                           apartment.getProperty("room1Dimensions").Value.ToString(),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room2").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room2").Value.ToString()),
                                                                           apartment.getProperty("room2Dimensions").Value.ToString(),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room3").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room3").Value.ToString()),
                                                                           apartment.getProperty("room3Dimensions").Value.ToString(),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room4").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room4").Value.ToString()),
                                                                           apartment.getProperty("room4Dimensions").Value.ToString(),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room5").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room5").Value.ToString()),
                                                                           apartment.getProperty("room5Dimensions").Value.ToString(),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room6").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room6").Value.ToString()),
                                                                           apartment.getProperty("room6Dimensions").Value.ToString(),
                                                                           string.IsNullOrEmpty(apartment.getProperty("room7").Value.ToString()) == true ? -1 : Convert.ToInt32(apartment.getProperty("room7").Value.ToString()),
                                                                           apartment.getProperty("room7Dimensions").Value.ToString(),
                                                                           apartment.getProperty("totalArea").Value.ToString(),
                                                                           apartment.getProperty("projectArea").Value.ToString()));
                                }
                            }
                        }
                    }
                }
            }

            return apartmentsCollection;
        }

唯一的问题是这需要8-10秒左右(即使只有约150个节点的顶​​部)太长了。

请记住,这只是用于检索原始集合。然后我使用一些linq语句过滤掉结果,这需要几秒钟。最终这导致搜索约150个节点大约需要15秒,这太长了。

如何加快速度?

由于

1 个答案:

答案 0 :(得分:4)

使用NodeFactory的节点访问您的数据而不是文档。文档访问数据库,这是性能问题的来源。 NodeFactory访问Umbraco缓存并且速度更快。有关详细信息,请参阅Difference between Node and Document

另一件需要注意的事情是缓存只包含已发布的节点。因此,您无需进行任何检查以查看它们是否已发布。