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;
}
// 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 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;
}
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.
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
public static IEnumerable
private static Domain GetDomain(int nodeId = -1) { if (nodeId == -1) nodeId = uQuery.GetCurrentNode().Id;
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);
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
Faster code will be something like that :
2) GetArticles method I would rewrite like that:
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
thank you so much for your suggestions, I'll try in my free time :)
Emiliano, I hope it will solve your problem.
Please, share your results with community.
Have a nice day.
Thanks,
Alex
is working on a reply...