Merge "NotifUser: Reduce queries in resetNotificationCount() by hackily computing some values from others"

This commit is contained in:
jenkins-bot 2016-05-03 13:43:42 +00:00 committed by Gerrit Code Review
commit a966e1825f

View file

@ -440,30 +440,48 @@ class MWEchoNotifUser {
* @param $dbSource int use master or slave database to pull count * @param $dbSource int use master or slave database to pull count
*/ */
public function resetNotificationCount( $dbSource = DB_SLAVE ) { public function resetNotificationCount( $dbSource = DB_SLAVE ) {
// TODO: Reuse information while recomputing these values. all=alert+messages and global=local+foreign
// Reset notification count for all sections
$this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALL, false );
// Reset alert and message counts, and store them for later // Reset alert and message counts, and store them for later
$alertCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALERT, false ); $alertCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALERT, false );
$msgCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::MESSAGE, false ); $msgCount = $this->getNotificationCount( false, $dbSource, EchoAttributeManager::MESSAGE, false );
// Reset global notification counts too // For performance, compute the ALL count by adding alerts and messages
// We're going to update echo_unread_wikis in a DeferredUpdate below, but that doesn't $allCount = $alertCount + $msgCount;
// affect the results of this computation, because we're only updating the echo_unread_wikis
// row for the current wiki, which is ignored by the recomputation code // For performance, compute the global counts by adding foreign counts to the above
$this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALL, true ); $globalAlertCount = $alertCount + $this->getForeignNotifications()->getCount( EchoAttributeManager::ALERT );
$this->getNotificationCount( false, $dbSource, EchoAttributeManager::ALERT, true ); $globalMsgCount = $msgCount + $this->getForeignNotifications()->getCount( EchoAttributeManager::MESSAGE );
$this->getNotificationCount( false, $dbSource, EchoAttributeManager::MESSAGE, true ); $globalAllCount = $globalAlertCount + $globalMsgCount;
// When notification counts need to be updated, the last notification may have changed, // When notification counts need to be updated, the last notification may have changed,
// so we also need to recompute the cached timestamp values. // so we also need to recompute the cached timestamp values.
$this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::ALL, false );
$alertUnread = $this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::ALERT, false ); $alertUnread = $this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::ALERT, false );
$msgUnread = $this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::MESSAGE, false ); $msgUnread = $this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::MESSAGE, false );
// Also recompute the global timestamps // For performance, compute the ALL count as the highest of these two
$this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::ALL, true ); $allUnread = $alertUnread !== false &&
$this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::ALERT, true ); ( $msgUnread === false || $alertUnread->diff( $msgUnread )->invert === 1 ) ?
$this->getLastUnreadNotificationTime( false, $dbSource, EchoAttributeManager::MESSAGE, true ); $alertUnread : $msgUnread;
// For performance, compute the global timestamps as max( localTimestamp, foreignTimestamp )
$foreignAlertUnread = $this->getForeignNotifications()->getTimestamp( EchoAttributeManager::ALERT );
$globalAlertUnread = $alertUnread !== false &&
( $foreignAlertUnread === false || $alertUnread->diff( $foreignAlertUnread )->invert === 1 ) ?
$alertUnread : $foreignAlertUnread;
$foreignMsgUnread = $this->getForeignNotifications()->getTimestamp( EchoAttributeManager::MESSAGE );
$globalMsgUnread = $msgUnread !== false &&
( $foreignMsgUnread === false || $msgUnread->diff( $foreignMsgUnread )->invert === 1 ) ?
$msgUnread : $foreignMsgUnread;
$globalAllUnread = $globalAlertUnread !== false &&
( $globalMsgUnread === false || $globalAlertUnread->diff( $globalMsgUnread )->invert === 1 ) ?
$globalAlertUnread : $globalMsgUnread;
// Write computed values to cache
$this->cache->set( $this->getMemcKey( 'echo-notification-count' ), $allCount, 86400 );
$this->cache->set( $this->getGlobalMemcKey( 'echo-notification-count-alert' ), $globalAlertCount, 86400 );
$this->cache->set( $this->getGlobalMemcKey( 'echo-notification-count-message' ), $globalMsgCount, 86400 );
$this->cache->set( $this->getGlobalMemcKey( 'echo-notification-count' ), $globalAllCount, 86400 );
$this->cache->set( $this->getMemcKey( 'echo-notification-timestamp', $allUnread === false ? -1 : $allUnread->getTimestamp( TS_MW ) ), 86400 );
$this->cache->set( $this->getGlobalMemcKey( 'echo-notification-timestamp-alert' ), $globalAlertUnread === false ? -1 : $globalAlertUnread->getTimestamp( TS_MW ), 86400 );
$this->cache->set( $this->getGlobalMemcKey( 'echo-notification-timestamp-message' ), $globalMsgUnread === false ? -1 : $globalMsgUnread->getTimestamp( TS_MW ), 86400 );
$this->cache->set( $this->getGlobalMemcKey( 'echo-notification-timestamp' ), $globalAllUnread === false ? -1 : $globalAllUnread->getTimestamp( TS_MW ), 86400 );
// Invalidate the user's cache // Invalidate the user's cache
$user = $this->mUser; $user = $this->mUser;