Stream of notices linking to an URL

This commit is contained in:
Evan Prodromou 2010-12-28 11:58:55 -08:00
parent c8bbde69df
commit bf4c5cb41a
2 changed files with 80 additions and 0 deletions

View File

@ -412,4 +412,69 @@ class File extends Memcached_DataObject
{ {
return File_thumbnail::staticGet('file_id', $this->id); return File_thumbnail::staticGet('file_id', $this->id);
} }
function blowCache($last=false)
{
self::blow('file:notice-ids:%s', $this->url);
if ($last) {
self::blow('file:notice-ids:%s;last', $this->url);
}
}
/**
* Stream of notices linking to this URL
*
* @param integer $offset Offset to show; default is 0
* @param integer $limit Limit of notices to show
* @param integer $since_id Since this notice
* @param integer $max_id Before this notice
*
* @return array ids of notices that link to this file
*/
function stream($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
{
$ids = Notice::stream(array($this, '_streamDirect'),
null,
'file:notice-ids:'.$this->url,
$offset, $limit, $since_id, $max_id);
return $ids;
}
/**
* Stream of notices linking to this URL
*
* @param integer $offset Offset to show; default is 0
* @param integer $limit Limit of notices to show
* @param integer $since_id Since this notice
* @param integer $max_id Before this notice
*
* @return array ids of notices that link to this file
*/
function _streamDirect($offset, $limit, $since_id, $max_id)
{
$f2p = new File_to_post();
$f2p->file_id = $this->id;
Notice::addWhereSinceId($f2p, $since_id, 'post_id', 'modified');
Notice::addWhereMaxId($f2p, $max_id, 'post_id', 'modified');
$f2p->orderBy('modified DESC, notice_id DESC');
if (!is_null($offset)) {
$reply->limit($offset, $limit);
}
$ids = array();
if ($f2p->find()) {
while ($f2p->fetch()) {
$ids[] = $f2p->notice_id;
}
}
return $ids;
}
} }

View File

@ -52,6 +52,12 @@ class File_to_post extends Memcached_DataObject
$f2p->file_id = $file_id; $f2p->file_id = $file_id;
$f2p->post_id = $notice_id; $f2p->post_id = $notice_id;
$f2p->insert(); $f2p->insert();
$f = File::staticGet($file_id);
if (!empty($f)) {
$f->blowNoticeCache();
}
} }
if (empty($seen[$notice_id])) { if (empty($seen[$notice_id])) {
@ -66,4 +72,13 @@ class File_to_post extends Memcached_DataObject
{ {
return Memcached_DataObject::pkeyGet('File_to_post', $kv); return Memcached_DataObject::pkeyGet('File_to_post', $kv);
} }
function delete()
{
$f = File::staticGet('id', $this->file_id);
if (!empty($f)) {
$f->blowNoticeCache();
}
return parent::delete();
}
} }