我有一個評論式結構,用戶能夠進行回覆的文章。 (一篇文章可以有很多討論回覆)。當用戶添加回復時,我希望父項文章的最後更新日期也發生變化,以便在從前端查看時,文章位於列表的頂部,表明它已具有最近的活動。爲了達到這個目的,評論是通過一個自定義控制器添加的,然後我使用ContentService發佈事件來更新父項,儘管我發現我的事件是一個瓶頸,需要長達六秒的時間才能運行一把umbraco發佈的事件表現
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += ContentServicePublished;
}
private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
foreach (var node in e.PublishedEntities)
{
//Handle updating the parent nodes last edited date to address ordering
if (node.ContentType.Alias == "DiscussionReply")
{
var contentService = new Umbraco.Core.Services.ContentService();
var parentNode = contentService.GetById(node.ParentId);
int intSiblings = parentNode.Children().Count() + 1;
if(parentNode.HasProperty("siblings"))
{
parentNode.SetValue("siblings", intSiblings);
contentService.SaveAndPublishWithStatus(parentNode, 0, false);
}
}
}
}
有什麼明顯的與此代碼可能會引發性能問題?
非常感謝,
我的猜測:你不應該爲每個節點創建一個新的contentService的,應在foreach之前只有一次完成... –