MediaWiki  REL1_22
ObjectCacheSessionHandler.php
Go to the documentation of this file.
00001 <?php
00030 class ObjectCacheSessionHandler {
00034     static function install() {
00035         session_set_save_handler(
00036             array( __CLASS__, 'open' ),
00037             array( __CLASS__, 'close' ),
00038             array( __CLASS__, 'read' ),
00039             array( __CLASS__, 'write' ),
00040             array( __CLASS__, 'destroy' ),
00041             array( __CLASS__, 'gc' ) );
00042 
00043         // It's necessary to register a shutdown function to call session_write_close(),
00044         // because by the time the request shutdown function for the session module is
00045         // called, $wgMemc has already been destroyed. Shutdown functions registered
00046         // this way are called before object destruction.
00047         register_shutdown_function( array( __CLASS__, 'handleShutdown' ) );
00048     }
00049 
00053     static function getCache() {
00054         global $wgSessionCacheType;
00055         return ObjectCache::getInstance( $wgSessionCacheType );
00056     }
00057 
00064     static function getKey( $id ) {
00065         return wfMemcKey( 'session', $id );
00066     }
00067 
00075     static function open( $save_path, $session_name ) {
00076         return true;
00077     }
00078 
00085     static function close() {
00086         return true;
00087     }
00088 
00095     static function read( $id ) {
00096         $data = self::getCache()->get( self::getKey( $id ) );
00097         if ( $data === false ) {
00098             return '';
00099         }
00100         return $data;
00101     }
00102 
00110     static function write( $id, $data ) {
00111         global $wgObjectCacheSessionExpiry;
00112         self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
00113         return true;
00114     }
00115 
00122     static function destroy( $id ) {
00123         self::getCache()->delete( self::getKey( $id ) );
00124         return true;
00125     }
00126 
00134     static function gc( $maxlifetime ) {
00135         return true;
00136     }
00137 
00142     static function handleShutdown() {
00143         session_write_close();
00144     }
00145 }