Optimize topic service
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
}
|
||||
var topicService = new TopicService(ApplicationContext.DatabaseContext);
|
||||
|
||||
Page<ReadOnlyTopic> topics;
|
||||
IEnumerable<ReadOnlyTopic> 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<ReadOnlyTopic>) cache.GetCacheItem(key,
|
||||
() => topicService.GetLatestTopics(50, page, true, cat),
|
||||
topics = (IEnumerable<ReadOnlyTopic>) cache.GetCacheItem(key,
|
||||
() => topicService.GetLatestTopics(50, page, true, cat).ToArray(),
|
||||
TimeSpan.FromSeconds(4));
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@
|
||||
<tbody class="forum-default-listing">
|
||||
|
||||
<!-- FORUM THREAD START -->
|
||||
@foreach (var topic in topics.Items)
|
||||
@foreach (var topic in topics)
|
||||
{
|
||||
|
||||
<tr class="@Umbraco.If(topic.Answer > 0, "solved")">
|
||||
|
||||
@@ -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<ExpandoObject> LatestPaged(int page, int cat)
|
||||
{
|
||||
var l = new List<ExpandoObject>();
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// <param name="ignoreSpam"></param>
|
||||
/// <param name="category"></param>
|
||||
/// <returns></returns>
|
||||
public Page<ReadOnlyTopic> GetLatestTopics(long take = 50, long page = 1, bool ignoreSpam = true, int category = -1)
|
||||
public IEnumerable<ReadOnlyTopic> 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<ReadOnlyTopic>(sql, new
|
||||
{
|
||||
sql.Where<Topic>(x => x.IsSpam != true);
|
||||
}
|
||||
|
||||
if (category > 0)
|
||||
sql.Where<Topic>(x => x.ParentId == category);
|
||||
|
||||
|
||||
sql.OrderByDescending("updated");
|
||||
return _databaseContext.Database.Page<ReadOnlyTopic>(page, take, sql);
|
||||
offset = (page - 1) * take,
|
||||
count = take,
|
||||
category = category
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user