Avoiding writing sessions for no reason

bug: T88635
Change-Id: I4afaecd8dc29390b1ee2a6a77f9ca0cba333ef92
This commit is contained in:
Aaron Schulz 2015-02-04 17:10:57 -08:00 committed by Legoktm
parent 69db9c034a
commit 532ef7851c

View file

@ -28,6 +28,9 @@
* @ingroup Cache * @ingroup Cache
*/ */
class ObjectCacheSessionHandler { class ObjectCacheSessionHandler {
/** @var array Map of (session ID => SHA-1 of the data) */
protected static $hashCache = array();
/** /**
* Install a session handler for the current web request * Install a session handler for the current web request
*/ */
@ -51,8 +54,9 @@ class ObjectCacheSessionHandler {
* Get the cache storage object to use for session storage * Get the cache storage object to use for session storage
* @return BagOStuff * @return BagOStuff
*/ */
static function getCache() { protected static function getCache() {
global $wgSessionCacheType; global $wgSessionCacheType;
return ObjectCache::getInstance( $wgSessionCacheType ); return ObjectCache::getInstance( $wgSessionCacheType );
} }
@ -62,10 +66,18 @@ class ObjectCacheSessionHandler {
* @param string $id Session id * @param string $id Session id
* @return string Cache key * @return string Cache key
*/ */
static function getKey( $id ) { protected static function getKey( $id ) {
return wfMemcKey( 'session', $id ); return wfMemcKey( 'session', $id );
} }
/**
* @param mixed $data
* @return string
*/
protected static function getHash( $data ) {
return sha1( serialize( $data ) );
}
/** /**
* Callback when opening a session. * Callback when opening a session.
* *
@ -95,10 +107,10 @@ class ObjectCacheSessionHandler {
*/ */
static function read( $id ) { static function read( $id ) {
$data = self::getCache()->get( self::getKey( $id ) ); $data = self::getCache()->get( self::getKey( $id ) );
if ( $data === false ) {
return ''; self::$hashCache = array( $id => self::getHash( $data ) );
}
return $data; return ( $data === false ) ? '' : $data;
} }
/** /**
@ -110,7 +122,14 @@ class ObjectCacheSessionHandler {
*/ */
static function write( $id, $data ) { static function write( $id, $data ) {
global $wgObjectCacheSessionExpiry; global $wgObjectCacheSessionExpiry;
// Only issue a write if anything changed (PHP 5.6 already does this)
if ( !isset( self::$hashCache[$id] )
|| self::getHash( $data ) !== self::$hashCache[$id]
) {
self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry ); self::getCache()->set( self::getKey( $id ), $data, $wgObjectCacheSessionExpiry );
}
return true; return true;
} }
@ -122,6 +141,7 @@ class ObjectCacheSessionHandler {
*/ */
static function destroy( $id ) { static function destroy( $id ) {
self::getCache()->delete( self::getKey( $id ) ); self::getCache()->delete( self::getKey( $id ) );
return true; return true;
} }