r103831 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103830‎ | r103831 | r103832 >
Date:20:35, 21 November 2011
Author:petrb
Status:deferred (Comments)
Tags:
Comment:
using cache now to avoid too many selects from db, thanks to Platonides and brion
Modified paths:
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.status.php (modified) (history)

Diff [purge]

Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.status.php
@@ -16,25 +16,65 @@
1717
1818 class OnlineStatusBar_StatusCheck {
1919
 20+ private static function getCacheKey( $user, $type ) {
 21+ // get a key for cache
 22+ return wfMemcKey( 'onlinestatusbarcache_' . $type . "_" . $user );
 23+ }
 24+
2025 /**
 26+ * Create a cache
 27+ * return true
 28+ */
 29+ public static function setCache( $user, $values, $type ) {
 30+ global $wgOnlineStatusBarWriteTime, $wgMemc;
 31+ // get a key
 32+ $cache_key = self::getCacheKey( $user, $type );
 33+ $wgMemc->set( $cache_key, $values, $wgOnlineStatusBarWriteTime );
 34+ return true;
 35+ }
 36+
 37+ public static function getCache( $user, $type ) {
 38+ global $wgMemc;
 39+ // get a key
 40+ $cache_key = self::getCacheKey( $user, $type );
 41+ // get a value
 42+ return $wgMemc->get( $cache_key );
 43+ }
 44+
 45+
 46+ /**
2147 * @param $user User
2248 * @return String
2349 */
2450 public static function getStatus( $user, $delayed_check = false ) {
25 - global $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline;
 51+ global $wgOnlineStatusBarDefaultOffline, $wgMemc, $wgOnlineStatusBarDefaultOnline;
2652
2753 // instead of delete every time just select the records which are not that old
28 - $dbr = wfGetDB( DB_SLAVE );
2954 if ( !$delayed_check ) {
3055 $t_time = OnlineStatusBar::getTimeoutDate();
31 - $result = $dbr->selectField( 'online_status', 'timestamp', array( 'username' => $user->getName(),
32 - "timestamp > " . $dbr->addQuotes( $dbr->timestamp( $t_time ) ) ),
33 - __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) );
 56+ // first try to use cache
 57+ $result = self::getCache( $user->getName(), "delayed" );
 58+
 59+ if ( $result == '' ) {
 60+ $dbr = wfGetDB( DB_SLAVE );
 61+ $result = $dbr->selectField( 'online_status', 'timestamp', array( 'username' => $user->getName(),
 62+ "timestamp > " . $dbr->addQuotes( $dbr->timestamp( $t_time ) ) ),
 63+ __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) );
 64+ // cache it
 65+ self::setCache( $user->getName(), $result, "delayed" );
 66+ }
3467 }
3568 else {
36 - $result = $dbr->selectField( 'online_status', 'timestamp', array( 'username' => $user->getName() ),
37 - __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) );
38 - $w_time = OnlineStatusBar::getTimeoutDate( true );
 69+ $result = self::getCache( $user->getName(), "normal" );
 70+
 71+ if ( $result == '' ) {
 72+ $dbr = wfGetDB( DB_SLAVE );
 73+ $result = $dbr->selectField( 'online_status', 'timestamp', array( 'username' => $user->getName() ),
 74+ __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) );
 75+ $w_time = OnlineStatusBar::getTimeoutDate( true );
 76+ // cache it
 77+ self::setCache( $user->getName(), $result, "normal" );
 78+ }
3979 }
4080
4181 if ( $result === false ) {

Comments

#Comment by YuviPanda (talk | contribs)   09:17, 23 November 2011

You don't have to concatenate stuff yourself for wfMemcKey - you just pass them as arguments and it does it for you.

#Comment by Catrope (talk | contribs)   10:28, 23 November 2011

Specifically, this means that something like

+		return wfMemcKey( 'onlinestatusbarcache_' . $type . "_" . $user );

should be

wfMemcKey( 'onlinestatusbar', $type, $user )

Status & tagging log