1

我已经编写了一个网站搜索,它遍历某个文件夹下的所有节点,然后根据某些条件过滤掉节点。该网站本身用于搜索公寓。这是我用于构建集合的功能:

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 秒,这太长了。

我怎样才能加快速度?

谢谢

4

1 回答 1

4

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

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

于 2012-11-08T03:34:13.693 回答