created timestamp on notice_inbox

darcs-hash:20081112172517-5ed1f-4e8534d7898e2134edf4c0a28417b4a5274617d4.gz
This commit is contained in:
Evan Prodromou 2008-11-12 12:25:17 -05:00
parent 8deac7248e
commit 3987de1064
5 changed files with 32 additions and 18 deletions

View File

@ -226,22 +226,25 @@ class Notice extends Memcached_DataObject
} }
} }
static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0) { # XXX: too many args; we need to move to named params or even a separate
# class for notice streams
static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $order=NULL) {
if (common_config('memcached', 'enabled')) { if (common_config('memcached', 'enabled')) {
# Skip the cache if this is a since_id or before_id qry # Skip the cache if this is a since_id or before_id qry
if ($since_id > 0 || $before_id > 0) { if ($since_id > 0 || $before_id > 0) {
return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id); return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order);
} else { } else {
return Notice::getCachedStream($qry, $cachekey, $offset, $limit); return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
} }
} }
return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id); return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order);
} }
static function getStreamDirect($qry, $offset, $limit, $since_id, $before_id) { static function getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order) {
$needAnd = FALSE; $needAnd = FALSE;
$needWhere = TRUE; $needWhere = TRUE;
@ -275,7 +278,13 @@ class Notice extends Memcached_DataObject
$qry .= ' notice.id < ' . $before_id; $qry .= ' notice.id < ' . $before_id;
} }
$qry .= ' ORDER BY notice.created DESC, notice.id DESC '; # Allow ORDER override
if ($order) {
$qry .= $order;
} else {
$qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
}
if (common_config('db','type') == 'pgsql') { if (common_config('db','type') == 'pgsql') {
$qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset; $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
@ -290,21 +299,20 @@ class Notice extends Memcached_DataObject
return $notice; return $notice;
} }
static function getCachedStream($qry, $cachekey, $offset, $limit) { static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
# If outside our cache window, just go to the DB # If outside our cache window, just go to the DB
if ($offset + $limit > NOTICE_CACHE_WINDOW) { if ($offset + $limit > NOTICE_CACHE_WINDOW) {
return Notice::getStreamDirect($qry, $offset, $limit); return Notice::getStreamDirect($qry, $offset, $limit, NULL, NULL, $order);
} }
# Get the cache; if we can't, just go to the DB # Get the cache; if we can't, just go to the DB
$cache = common_memcache(); $cache = common_memcache();
if (!$cache) { if (!$cache) {
return Notice::getStreamDirect($qry, $offset, $limit); return Notice::getStreamDirect($qry, $offset, $limit, NULL, NULL, $order);
} }
# Get the notices out of the cache # Get the notices out of the cache
@ -320,7 +328,7 @@ class Notice extends Memcached_DataObject
# Otherwise, get the full cache window out of the DB # Otherwise, get the full cache window out of the DB
$notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW); $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, NULL, NULL, $order);
# If there are no hits, just return the value # If there are no hits, just return the value

View File

@ -29,6 +29,7 @@ class Notice_inbox extends Memcached_DataObject
public $__table = 'notice_inbox'; // table name public $__table = 'notice_inbox'; // table name
public $user_id; // int(4) primary_key not_null public $user_id; // int(4) primary_key not_null
public $notice_id; // int(4) primary_key not_null public $notice_id; // int(4) primary_key not_null
public $created; // datetime() not_null
public $source; // tinyint(1) default_1 public $source; // tinyint(1) default_1
/* Static get */ /* Static get */

View File

@ -330,9 +330,12 @@ class User extends Memcached_DataObject
'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' . 'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
'WHERE notice_inbox.user_id = %d '; 'WHERE notice_inbox.user_id = %d ';
# NOTE: we override ORDER
return Notice::getStream(sprintf($qry, $this->id), return Notice::getStream(sprintf($qry, $this->id),
'user:notices_with_friends:' . $this->id, 'user:notices_with_friends:' . $this->id,
$offset, $limit, $since_id, $before_id); $offset, $limit, $since_id, $before_id,
'ORDER BY notice_inbox.created DESC, notice_inbox.id DESC ');
} }
function blowFavesCache() { function blowFavesCache() {

View File

@ -155,6 +155,7 @@ id = N
[notice_inbox] [notice_inbox]
user_id = 129 user_id = 129
notice_id = 129 notice_id = 129
created = 142
source = 17 source = 17
[notice_inbox__keys] [notice_inbox__keys]

View File

@ -336,6 +336,7 @@ create table notice_inbox (
user_id integer not null comment 'user receiving the message' references user (id), user_id integer not null comment 'user receiving the message' references user (id),
notice_id integer not null comment 'notice received' references notice (id), notice_id integer not null comment 'notice received' references notice (id),
created datetime not null comment 'date the notice was created',
source tinyint default 1 comment 'reason it is in the inbox; 1=subscription', source tinyint default 1 comment 'reason it is in the inbox; 1=subscription',
constraint primary key (user_id, notice_id), constraint primary key (user_id, notice_id),