Restored I4afaecd8: "Avoiding writing sessions for no reason"
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 11 Aug 2015 00:49:19 +0000 (17:49 -0700)
committerOri.livneh <ori@wikimedia.org>
Tue, 11 Aug 2015 21:46:05 +0000 (21:46 +0000)
* After 203d2c9c11643, the refresh updates should actually trigger

This reverts commit e48fec5a8ab5916735849d420f2dfeec7eb0fced.

Change-Id: I8427ed5b3a5bb80033cbdf071a33f8a3999ecb97

includes/objectcache/ObjectCacheSessionHandler.php

index 8a7b298..1f4beb9 100644 (file)
@@ -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;
        }