From 2b901894c2c14f3e7e1c3eae8960f8a3c09310a0 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 23 Mar 2011 11:59:01 -0400 Subject: [PATCH] Some fixes to make the notice stream class work --- classes/Fave.php | 10 ++++++++++ classes/Notice.php | 6 +++--- classes/Profile.php | 4 ++-- lib/noticestream.php | 10 ++++++---- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/classes/Fave.php b/classes/Fave.php index a61f35d190..7cd64982cd 100644 --- a/classes/Fave.php +++ b/classes/Fave.php @@ -87,6 +87,16 @@ class Fave extends Memcached_DataObject return $stream->getNotices($offset, $limit, $since_id, $max_id); } + function idStream($user_id, $offset=0, $limit=NOTICES_PER_PAGE, $own=false, $since_id=0, $max_id=0) + { + $stream = new NoticeStream(array('Fave', '_streamDirect'), + array($user_id, $own), + ($own) ? 'fave:ids_by_user_own:'.$user_id : + 'fave:ids_by_user:'.$user_id); + + return $stream->getNoticeIds($offset, $limit, $since_id, $max_id); + } + /** * Note that the sorting for this is by order of *fave* not order of *notice*. * diff --git a/classes/Notice.php b/classes/Notice.php index 8200e4554f..1201dd902b 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -45,7 +45,7 @@ require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; /* We keep 200 notices, the max number of notices available per API request, * in the memcached cache. */ -define('NOTICE_CACHE_WINDOW', 200); +define('NOTICE_CACHE_WINDOW', NoticeStream::CACHE_WINDOW); define('MAX_BOXCARS', 128); @@ -548,7 +548,7 @@ class Notice extends Memcached_DataObject if (empty($profile)) { return false; } - $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW); + $notice = $profile->getNotices(0, NoticeStream::CACHE_WINDOW); if (!empty($notice)) { $last = 0; while ($notice->fetch()) { @@ -1656,7 +1656,7 @@ class Notice extends Memcached_DataObject } } - return Notice::getStreamByIds($ids); + return NoticeStream::getStreamByIds($ids); } function _repeatStreamDirect($limit) diff --git a/classes/Profile.php b/classes/Profile.php index 209e5ef84a..c5dd2dfda9 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -550,7 +550,7 @@ class Profile extends Memcached_DataObject // This is the stream of favorite notices, in rev chron // order. This forces it into cache. - $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW); + $ids = Fave::idStream($this->id, 0, NoticeStream::CACHE_WINDOW); // If it's in the list, then it's a fave @@ -562,7 +562,7 @@ class Profile extends Memcached_DataObject // then the cache has all available faves, so this one // is not a fave. - if (count($ids) < NOTICE_CACHE_WINDOW) { + if (count($ids) < NoticeStream::CACHE_WINDOW) { return false; } diff --git a/lib/noticestream.php b/lib/noticestream.php index a96eb53da6..d1ed203a67 100644 --- a/lib/noticestream.php +++ b/lib/noticestream.php @@ -47,6 +47,8 @@ if (!defined('STATUSNET')) { class NoticeStream { + const CACHE_WINDOW = 200; + public $generator = null; public $args = null; public $cachekey = null; @@ -71,13 +73,13 @@ class NoticeStream { $cache = Cache::instance(); - // We cache NOTICE_CACHE_WINDOW elements at the tip of the stream. + // We cache self::CACHE_WINDOW elements at the tip of the stream. // If the cache won't be hit, just generate directly. if (empty($cache) || $sinceId != 0 || $maxId != 0 || is_null($limit) || - ($offset + $limit) > NOTICE_CACHE_WINDOW) { + ($offset + $limit) > self::CACHE_WINDOW) { return $this->generate($offset, $limit, $sinceId, $maxId); } @@ -105,7 +107,7 @@ class NoticeStream if ($laststr !== false) { $window = explode(',', $laststr); $last_id = $window[0]; - $new_ids = $this->generate(0, NOTICE_CACHE_WINDOW, $last_id, 0); + $new_ids = $this->generate(0, self::CACHE_WINDOW, $last_id, 0); $new_window = array_merge($new_ids, $window); @@ -122,7 +124,7 @@ class NoticeStream // No cache hits :( Generate directly and stick the results // into the cache. Note we generate the full cache window. - $window = $this->generate(0, NOTICE_CACHE_WINDOW, 0, 0); + $window = $this->generate(0, self::CACHE_WINDOW, 0, 0); $windowstr = implode(',', $window);