* @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
*/
*/
protected static function getCache() {
global $wgSessionCacheType;
+
return ObjectCache::getInstance( $wgSessionCacheType );
}
return wfMemcKey( 'session', $id );
}
+ /**
+ * @param mixed $data
+ * @return string
+ */
+ protected static function getHash( $data ) {
+ return sha1( serialize( $data ) );
+ }
+
/**
* Callback when opening a session.
*
*/
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;
}
/**
*/
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;
}