优化数据库连接以在 Umbraco CMS 中高效创建节点

问题描述 投票:0回答:0

我正在使用 Umbraco CMS 开发一个项目,其中我需要使用 ContentService 以编程方式创建大量节点(内容项)。但是,我面临性能问题,因为在 SaveAndPublish 和删除操作期间执行了许多数据库命令。节点创建和删除过程所花费的时间正在成为一个重要问题。

我尝试使用 IScopeProvider 的范围来批处理操作,但它没有提供预期的性能改进。

我正在寻求有关在 Umbraco CMS 中创建和删除大量节点时如何优化数据库连接并提高性能的建议。我想知道是否有任何最佳实践或替代方法可以用来加速这些操作。

具体来说,我有兴趣了解如何:

  1. 使用 ContentService 高效创建多个节点 最少的数据库命令。
  2. 优化删除流程,避免 过多的数据库命令并减少执行时间。

这是我的方法(我只提供重要部分,如果您需要更多、端点或其他内容,我将编辑并添加它)

 private void DeleteProducts(int count)
    {
        var productFolder = _umbracoHelper.GetNodeUnderRootByAlias(ContentTypeAlias.ProductsFolder);

        var productsNamesForDeletion = Enumerable.Range(0, count).Select(i => $"Product {i} SV");
        var products = GetProducts(productFolder.Id);
        var productsToDelete = products.Where(p => productsNamesForDeletion.Contains(p.Name));

        using var scope = _scopeProvider.CreateScope(isolationLevel: IsolationLevel.Serializable, repositoryCacheMode: RepositoryCacheMode.Default, autoComplete: true);
        
        foreach (var product in productsToDelete)
        {
            _contentService.Delete(product);
        }
        
        scope.Complete();
    }

private void CreateProducts(int count, (int productFolderId, IEnumerable<LanguageModel> languages, IContentBase productPage) setupData)
    {
        using var scope = _scopeProvider.CreateScope(autoComplete: true);
        
        Enumerable.Range(0, count)
            .ForEach(i => CreateAndPublishProduct(i, setupData));

        scope.Complete();
    }
 private void CreateAndPublishProduct(int index, (int productFolderId, IEnumerable<LanguageModel> languages, IContentBase productPage) setupData)
    {
    var product = CreateProduct(index, setupData);

    _contentService.SaveAndPublish(product);
    }

任何有关改进当前实现的代码示例或建议将不胜感激。预先感谢您的帮助!

c# umbraco
© www.soinside.com 2019 - 2024. All rights reserved.