Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Emiliano Magliocca 2 posts 72 karma points
    Oct 06, 2016 @ 10:27
    Emiliano Magliocca
    0

    Timout after trying to load articles

    Hello guys,

    I have a problem when I try to load my paginated articles I got a timeout error, probably because now there are too many articles in that section, can you kindly try to help me to figure it out what's the best way to optimize my code? you can find it below. thanks

      private int GetRootNodeByLanguage(string language)
        {
            var i = language.Equals("en-US") ? 0 : 1;
            return UmbracoCommon.Content.GetContentByType("feedItemList").ElementAt(i).Id;
        }
    
    public IEnumerable<IContent> GetArticles(string language = null, string page = null)
        {
            if (language == null) language = UmbracoCommon.Language.GetDomainLanguage();
    
            var nodeId = GetRootNodeByLanguage(language);
    
            var allArticlesCount = ApplicationContext.Current.Services.ContentService.GetById(nodeId)
                            .Descendants().Count();
    
            var paging = PagingHelper.GetPages(allArticlesCount, 20, page);
    
            var selectedArticles = ApplicationContext.Current.Services.ContentService.GetById(nodeId)
                            .Descendants().Skip(paging.Skip).Take(paging.Take).ToList();
    
            return selectedArticles;
        }
    

    public static IEnumerable

        {
            return uQuery.GetNodesByType(type);
        }
    

    private static Domain GetDomain(int nodeId = -1) { if (nodeId == -1) nodeId = uQuery.GetCurrentNode().Id;

            // get the domains for the node Id
            var domains = library.GetCurrentDomains(nodeId);
    
            // check that a domain exists
            if (domains != null && domains.Length > 0)
            {
                // return the language id from the first domain
                return domains[0];
            }
    
            return null;
        }
    

    public static string GetDomainLanguage(int nodeId = -1) { return GetDomain(nodeId).Language.CultureAlias; }

    public static Paging GetPages(int itemCount, int itemsPerPage, string page = null) { int currentPage; int.TryParse(HttpContext.Current.Request.QueryString["page"], out currentPage);

            if (page != null) int.TryParse(page, out currentPage);
    
            if (currentPage < 1) currentPage = 1;
    
            var pages = new Paging
            {
                ItemsPerPage = itemsPerPage,
                CurrentPage = currentPage,
                PreviousPage = currentPage - 1,
                NextPage = currentPage + 1,
                TotalPages = (int) Math.Ceiling((double) itemCount/itemsPerPage),
                Skip = itemsPerPage*currentPage - itemsPerPage,
                Take = itemsPerPage
            };
    
            return pages;
        }
    
  • Alex Skrypnyk 6148 posts 24097 karma points MVP 8x admin c-trib
    Oct 06, 2016 @ 11:48
    Alex Skrypnyk
    101

    Hi Emiliano,

    Welcome to our forum and I recommend you few steps:

    1) Do not use uQuery in any case, it's really slow library, try to avoid it

    public static IEnumerable GetContentByType(string type)
    {
        return uQuery.GetNodesByType(type);
    }
    

    Faster code will be something like that :

    var query = ExamineManager.Instance.CreateSearchCriteria()
                              .NodeTypeAlias("yourDocumentType")
                              .Compile();
    
    IEnumerable<IPublishedContent> myNodes = Umbraco.TypedSearch(query);
    

    2) GetArticles method I would rewrite like that:

    public IEnumerable<IContent> GetArticles(string language = null, string page = null)
            {
                if (language == null) language = UmbracoCommon.Language.GetDomainLanguage();
    
                var nodeId = GetRootNodeByLanguage(language);
    
                var descendants = ApplicationContext.Current.Services.ContentService.GetById(nodeId)
                    .Descendants();
    
                var allArticlesCount = descendants.Count();
    
                var paging = PagingHelper.GetPages(allArticlesCount, 20, page);
    
                var selectedArticles = descendants.Skip(paging.Skip).Take(paging.Take).ToList();
    
                return selectedArticles;
            }
    

    I removed one call of Descendants() method - it's the heaviest method in Umbraco, if you know tree structure you can use Children.Children for example, what is much faster.

    3) Also I recommend you to rewrite GetRootNodeByLanguage, GetContentByType - looks like heavy method too.

    I hope it all make sense and will help you.

    Thanks,

    Alex

  • Emiliano Magliocca 2 posts 72 karma points
    Oct 06, 2016 @ 12:47
    Emiliano Magliocca
    0

    thank you so much for your suggestions, I'll try in my free time :)

  • Alex Skrypnyk 6148 posts 24097 karma points MVP 8x admin c-trib
    Oct 06, 2016 @ 12:49
    Alex Skrypnyk
    0

    Emiliano, I hope it will solve your problem.

    Please, share your results with community.

    Have a nice day.

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft