Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.status.php |
— | — | @@ -16,25 +16,65 @@ |
17 | 17 | |
18 | 18 | class OnlineStatusBar_StatusCheck { |
19 | 19 | |
| 20 | + private static function getCacheKey( $user, $type ) { |
| 21 | + // get a key for cache |
| 22 | + return wfMemcKey( 'onlinestatusbarcache_' . $type . "_" . $user ); |
| 23 | + } |
| 24 | + |
20 | 25 | /** |
| 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 | + /** |
21 | 47 | * @param $user User |
22 | 48 | * @return String |
23 | 49 | */ |
24 | 50 | public static function getStatus( $user, $delayed_check = false ) { |
25 | | - global $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline; |
| 51 | + global $wgOnlineStatusBarDefaultOffline, $wgMemc, $wgOnlineStatusBarDefaultOnline; |
26 | 52 | |
27 | 53 | // instead of delete every time just select the records which are not that old |
28 | | - $dbr = wfGetDB( DB_SLAVE ); |
29 | 54 | if ( !$delayed_check ) { |
30 | 55 | $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 | + } |
34 | 67 | } |
35 | 68 | 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 | + } |
39 | 79 | } |
40 | 80 | |
41 | 81 | if ( $result === false ) { |