make faves work with ids

This commit is contained in:
Evan Prodromou 2009-05-01 12:01:28 -07:00
parent 021b520a11
commit 5314d9b2cf
3 changed files with 70 additions and 25 deletions

View File

@ -4,7 +4,7 @@
*/
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
class Fave extends Memcached_DataObject
class Fave extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
@ -31,9 +31,58 @@ class Fave extends Memcached_DataObject
}
return $fave;
}
function &pkeyGet($kv)
{
return Memcached_DataObject::pkeyGet('Fave', $kv);
}
function stream($user_id, $offset=0, $limit=NOTICES_PER_PAGE)
{
$ids = Notice::stream(array('Fave', '_streamDirect'),
array($user_id),
'fave:ids_by_user:'.$user_id,
$offset, $limit);
return $ids;
}
function _streamDirect($user_id, $offset, $limit, $since_id, $before_id, $since)
{
$fav = new Fave();
$fav->user_id = $user_id;
$fav->selectAdd();
$fav->selectAdd('notice_id');
if ($since_id != 0) {
$fav->whereAdd('notice_id > ' . $since_id);
}
if ($before_id != 0) {
$fav->whereAdd('notice_id < ' . $before_id);
}
if (!is_null($since)) {
$fav->whereAdd('modified > \'' . date('Y-m-d H:i:s', $since) . '\'');
}
// NOTE: we sort by fave time, not by notice time!
$fav->orderBy('modified DESC');
if (!is_null($offset)) {
$fav->limit($offset, $limit);
}
$ids = array();
if ($fav->find()) {
while ($fav->fetch()) {
$ids[] = $fav->notice_id;
}
}
return $ids;
}
}

View File

@ -412,9 +412,9 @@ class Notice extends Memcached_DataObject
$fave->notice_id = $this->id;
if ($fave->find()) {
while ($fave->fetch()) {
$cache->delete(common_cache_key('user:faves:'.$fave->user_id));
$cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
if ($blowLast) {
$cache->delete(common_cache_key('user:faves:'.$fave->user_id.';last'));
$cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
}
}
}

View File

@ -349,30 +349,31 @@ class User extends Memcached_DataObject
$cache = common_memcache();
// XXX: Kind of a hack.
if ($cache) {
// This is the stream of favorite notices, in rev chron
// order. This forces it into cache.
$faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
$cnt = 0;
while ($faves->fetch()) {
if ($faves->id < $notice->id) {
// If we passed it, it's not a fave
return false;
} else if ($faves->id == $notice->id) {
// If it matches a cached notice, then it's a fave
return true;
}
$cnt++;
$ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
// If it's in the list, then it's a fave
if (in_array($notice->id, $ids)) {
return true;
}
// If we're not past the end of the cache window,
// then the cache has all available faves, so this one
// is not a fave.
if ($cnt < NOTICE_CACHE_WINDOW) {
if (count($ids) < NOTICE_CACHE_WINDOW) {
return false;
}
// Otherwise, cache doesn't have all faves;
// fall through to the default
}
$fave = Fave::pkeyGet(array('user_id' => $this->id,
'notice_id' => $notice->id));
return ((is_null($fave)) ? false : true);
@ -418,13 +419,8 @@ class User extends Memcached_DataObject
function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
{
$qry =
'SELECT notice.* ' .
'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
'WHERE fave.user_id = %d ';
return Notice::getStream(sprintf($qry, $this->id),
'user:faves:'.$this->id,
$offset, $limit);
$ids = Fave::stream($this->id, $offset, $limit);
return Notice::getStreamByIds($ids);
}
function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
@ -459,8 +455,8 @@ class User extends Memcached_DataObject
if ($cache) {
// Faves don't happen chronologically, so we need to blow
// ;last cache, too
$cache->delete(common_cache_key('user:faves:'.$this->id));
$cache->delete(common_cache_key('user:faves:'.$this->id).';last');
$cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
$cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
}
}