From 90a7631592858f5ac2c46e459e04b541960a9a30 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 17 Mar 2011 17:36:53 -0700 Subject: [PATCH] Caching for conversation root lookup, some logic fixes in threaded view --- classes/Fave.php | 8 ++++---- classes/Notice.php | 34 ++++++++++++++++++++++++++++++++++ lib/threadednoticelist.php | 14 +++++--------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/classes/Fave.php b/classes/Fave.php index e4e00d27ca..efbceee6a8 100644 --- a/classes/Fave.php +++ b/classes/Fave.php @@ -44,7 +44,7 @@ class Fave extends Memcached_DataObject common_log_db_error($fave, 'INSERT', __FILE__); return false; } - self::blow('fave:by_notice', $fave->notice_id); + self::blow('fave:by_notice:%d', $fave->notice_id); Event::handle('EndFavorNotice', array($profile, $notice)); } @@ -62,7 +62,7 @@ class Fave extends Memcached_DataObject if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) { $result = parent::delete(); - self::blow('fave:by_notice', $this->notice_id); + self::blow('fave:by_notice:%d', $this->notice_id); if ($result) { Event::handle('EndDisfavorNotice', array($profile, $notice)); @@ -219,7 +219,7 @@ class Fave extends Memcached_DataObject static function byNotice($noticeId) { $c = self::memcache(); - $key = Cache::key('fave:by_notice', $noticeId); + $key = Cache::key('fave:by_notice:' . $noticeId); $wrapper = $c->get($key); if (!$wrapper) { @@ -228,7 +228,7 @@ class Fave extends Memcached_DataObject $fave->notice_id = $noticeId; $fave->find(); - $profiles = array(); + $list = array(); while ($fave->fetch()) { $list[] = clone($fave); } diff --git a/classes/Notice.php b/classes/Notice.php index 560d45a640..a158a4930d 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -498,6 +498,11 @@ class Notice extends Memcached_DataObject } self::blow('fave:by_notice', $this->id); + + if ($this->conversation) { + // In case we're the first, will need to calc a new root. + self::blow('notice:conversation_root:%d', $this->conversation); + } } /** save all urls in the notice to the db @@ -776,6 +781,35 @@ class Notice extends Memcached_DataObject return false; } + /** + * Grab the earliest notice from this conversation. + * + * @return Notice or null + */ + function conversationRoot() + { + if (!empty($this->conversation)) { + $c = self::memcache(); + + $key = Cache::key('notice:conversation_root:' . $this->conversation); + $notice = $c->get($key); + if ($notice) { + return $notice; + } + + $notice = new Notice(); + $notice->conversation = $this->conversation; + $notice->orderBy('CREATED'); + $notice->limit(1); + $notice->find(true); + + if ($notice->N) { + $c->set($key, $notice); + return $notice; + } + } + return null; + } /** * Pull up a full list of local recipients who will be getting * this notice in their inbox. Results will be cached, so don't diff --git a/lib/threadednoticelist.php b/lib/threadednoticelist.php index c6fe77ddb4..e8f0d0c8d1 100644 --- a/lib/threadednoticelist.php +++ b/lib/threadednoticelist.php @@ -91,17 +91,13 @@ class ThreadedNoticeList extends NoticeList $conversations[$convo] = true; // Get the convo's root notice - // @fixme stream goes in wrong direction, this needs sane caching - //$notice = Notice::conversationStream($convo, 0, 1); - //$notice->fetch(); - $root = new Notice(); - $root->conversation = $notice->conversation; - $root->orderBy('CREATED'); - $root->limit(1); - $root->find(true); + $root = $notice->conversationRoot(); + if ($root) { + $notice = $root; + } try { - $item = $this->newListItem($root); + $item = $this->newListItem($notice); $item->show(); } catch (Exception $e) { // we log exceptions and continue -- 2.39.5