From: Aaron Schulz Date: Tue, 11 Aug 2015 00:49:19 +0000 (-0700) Subject: Restored I4afaecd8: "Avoiding writing sessions for no reason" X-Git-Tag: 1.31.0-rc.0~10453 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/categories/modifier.php?a=commitdiff_plain;h=08039b2df44a2e83974d77fccba337a5d86cd854;p=lhc%2Fweb%2Fwiklou.git Restored I4afaecd8: "Avoiding writing sessions for no reason" * After 203d2c9c11643, the refresh updates should actually trigger This reverts commit e48fec5a8ab5916735849d420f2dfeec7eb0fced. Change-Id: I8427ed5b3a5bb80033cbdf071a33f8a3999ecb97 --- diff --git a/includes/objectcache/ObjectCacheSessionHandler.php b/includes/objectcache/ObjectCacheSessionHandler.php index 8a7b2980b6..1f4beb9dec 100644 --- a/includes/objectcache/ObjectCacheSessionHandler.php +++ b/includes/objectcache/ObjectCacheSessionHandler.php @@ -30,6 +30,9 @@ use MediaWiki\Logger\LoggerFactory; * @ingroup Cache */ 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 */ @@ -55,6 +58,7 @@ class ObjectCacheSessionHandler { */ protected static function getCache() { global $wgSessionCacheType; + return ObjectCache::getInstance( $wgSessionCacheType ); } @@ -68,6 +72,14 @@ class ObjectCacheSessionHandler { return wfMemcKey( 'session', $id ); } + /** + * @param mixed $data + * @return string + */ + protected static function getHash( $data ) { + return sha1( serialize( $data ) ); + } + /** * Callback when opening a session. * @@ -97,10 +109,10 @@ class ObjectCacheSessionHandler { */ static function read( $id ) { $data = self::getCache()->get( self::getKey( $id ) ); - if ( $data === false ) { - return ''; - } - return $data; + + self::$hashCache = array( $id => self::getHash( $data ) ); + + return ( $data === false ) ? '' : $data; } /** @@ -112,7 +124,14 @@ class ObjectCacheSessionHandler { */ static function write( $id, $data ) { global $wgObjectCacheSessionExpiry; - self::getCache()->set( self::getKey( $id ), $data, $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 ); + } + return true; }