示例#1
0
 /**
  * Append the given notice to the given user's inbox.
  * Caching updates are managed for the inbox itself.
  *
  * If the notice is already in this inbox, the second
  * add will be silently dropped.
  *
  * @param int @user_id
  * @param int $notice_id
  * @return boolean success
  */
 static function insertNotice($user_id, $notice_id)
 {
     // Going straight to the DB rather than trusting our caching
     // during an update. Note: not using DB_DataObject::staticGet,
     // which is unsafe to use directly (in-process caching causes
     // memory leaks, which accumulate in queue processes).
     $inbox = new Inbox();
     if (!$inbox->get('user_id', $user_id)) {
         $inbox = Inbox::initialize($user_id);
     }
     if (empty($inbox)) {
         return false;
     }
     $ids = $inbox->unpack();
     if (in_array(intval($notice_id), $ids)) {
         // Already in there, we probably re-ran some inbox adds
         // due to an error. Skip the dupe silently.
         return true;
     }
     $result = $inbox->query(sprintf('UPDATE inbox ' . 'set notice_ids = concat(cast(0x%08x as binary(4)), ' . 'substr(notice_ids, 1, %d)) ' . 'WHERE user_id = %d', $notice_id, 4 * (self::MAX_NOTICES - 1), $user_id));
     if ($result) {
         self::blow('inbox:user_id:%d', $user_id);
     }
     return $result;
 }