From fdfc49d01f063db09bb9d46b120ff0fc282ecdb1 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 2 Jun 2015 17:15:01 +0200 Subject: [PATCH] Optimize topic service --- .../Views/MacroPartials/Forum/Forum.cshtml | 8 ++-- uForum/Api/PublicForumController.cs | 41 ++++++++++++------- uForum/Services/TopicService.cs | 41 ++++++++++++------- 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/OurUmbraco.Site/Views/MacroPartials/Forum/Forum.cshtml b/OurUmbraco.Site/Views/MacroPartials/Forum/Forum.cshtml index 67c3c83a..94ad9c22 100644 --- a/OurUmbraco.Site/Views/MacroPartials/Forum/Forum.cshtml +++ b/OurUmbraco.Site/Views/MacroPartials/Forum/Forum.cshtml @@ -19,7 +19,7 @@ } var topicService = new TopicService(ApplicationContext.DatabaseContext); - Page topics; + IEnumerable topics; // only cache the first page of each category if (cat > 0 || page > 1) @@ -30,8 +30,8 @@ { var cache = ApplicationContext.Current.ApplicationCache.RuntimeCache; var key = "OurForumForum[" + cat + "]"; - topics = (Page) cache.GetCacheItem(key, - () => topicService.GetLatestTopics(50, page, true, cat), + topics = (IEnumerable) cache.GetCacheItem(key, + () => topicService.GetLatestTopics(50, page, true, cat).ToArray(), TimeSpan.FromSeconds(4)); } } @@ -66,7 +66,7 @@ - @foreach (var topic in topics.Items) + @foreach (var topic in topics) { diff --git a/uForum/Api/PublicForumController.cs b/uForum/Api/PublicForumController.cs index de9fefb3..2ac699c3 100644 --- a/uForum/Api/PublicForumController.cs +++ b/uForum/Api/PublicForumController.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using System.Web.Http; using uForum.Services; +using Umbraco.Core.Models; using Umbraco.Web.WebApi; namespace uForum.Api @@ -18,7 +19,7 @@ namespace uForum.Api public IEnumerable LatestPaged(int page, int cat) { var l = new List(); - foreach (var topic in TopicService.GetLatestTopics(50, page, true, cat).Items) + foreach (var topic in TopicService.GetLatestTopics(50, page, true, cat)) { dynamic o = new ExpandoObject(); @@ -29,29 +30,39 @@ namespace uForum.Api o.updated = topic.Updated.ConvertToRelativeTime(); if (topic.LatestReplyAuthor > 0) { - var mem = Members.GetById(topic.LatestReplyAuthor); - if (mem != null) - { - o.memId = mem.Id; - o.memName = mem.Name; - } + o.memId = topic.LatestReplyAuthor; + o.memName = topic.LastReplyAuthorName; + //var mem = Members.GetById(topic.LatestReplyAuthor); + //if (mem != null) + //{ + // o.memId = mem.Id; + // o.memName = mem.Name; + //} } else { - var author = Members.GetById(topic.MemberId); - if (author != null) - { - o.memId = author.Id; - o.memName = author.Name; - } + o.memId = topic.MemberId; + o.memName = topic.AuthorName; + //var author = Members.GetById(topic.MemberId); + //if (author != null) + //{ + // o.memId = author.Id; + // o.memName = author.Name; + //} } - var forum = Umbraco.TypedContent(topic.ParentId); - if (forum != null) + IPublishedContent forum; + if (topic.ParentId > 0 && ((forum = Umbraco.TypedContent(topic.ParentId)) != null)) { o.forumUrl = forum.Url; o.forumName = forum.Name; } + //var forum = Umbraco.TypedContent(topic.ParentId); + //if (forum != null) + //{ + // o.forumUrl = forum.Url; + // o.forumName = forum.Name; + //} l.Add(o); } diff --git a/uForum/Services/TopicService.cs b/uForum/Services/TopicService.cs index c04400a2..1e6bd6d1 100644 --- a/uForum/Services/TopicService.cs +++ b/uForum/Services/TopicService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data.SqlTypes; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -28,24 +29,34 @@ namespace uForum.Services /// /// /// - public Page GetLatestTopics(long take = 50, long page = 1, bool ignoreSpam = true, int category = -1) + public IEnumerable GetLatestTopics(long take = 50, long page = 1, bool ignoreSpam = true, int category = -1) { - var sql = new Sql().Select(@"forumTopics.*, u1.[text] as LastReplyAuthorName, u2.[text] as AuthorName") - .From("forumTopics") - .LeftOuterJoin("umbracoNode u1").On("(forumTopics.latestReplyAuthor = u1.id AND u1.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560')") - .LeftOuterJoin("umbracoNode u2").On("(forumTopics.memberId = u2.id AND u2.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560')"); + const string sql1 = @"SELECT forumTopics.*, u1.[text] as LastReplyAuthorName, u2.[text] as AuthorName +FROM forumTopics +LEFT OUTER JOIN umbracoNode u1 ON (forumTopics.latestReplyAuthor = u1.id AND u1.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560') +LEFT OUTER JOIN umbracoNode u2 ON (forumTopics.memberId = u2.id AND u2.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560') +"; + const string sql2 = @" +ORDER BY updated +OFFSET @offset ROWS +FETCH NEXT @count ROWS ONLY"; - if (ignoreSpam) + const string sqlxx = sql1 + sql2; + const string sqlix = sql1 + "WHERE isSpam=0" + sql2; + const string sqlxc = sql1 + "WHERE forumTopics.parentId=@category" + sql2; + const string sqlic = sql1 + "WHERE isSpam=0 AND forumTopics.parentId=@category" + sql2; + + var sql = ignoreSpam + ? (category > 0 ? sqlic : sqlix) + : (category > 0 ? sqlxc : sqlxx); + + // probably as fast as PetaPoco can be... + return _databaseContext.Database.Fetch(sql, new { - sql.Where(x => x.IsSpam != true); - } - - if (category > 0) - sql.Where(x => x.ParentId == category); - - - sql.OrderByDescending("updated"); - return _databaseContext.Database.Page(page, take, sql); + offset = (page - 1) * take, + count = take, + category = category + }); } ///