+ /**
+ * Shortcut to update caches.
+ *
+ * @param $cache Array: cached messages with a version.
+ * @param $cacheKey String: Identifier for the cache.
+ * @param $memc Bool: Wether to update or not memcache.
+ * @return False on somekind of error.
+ */
+ protected function saveToCaches( $cache, $cacheKey, $memc = true ) {
+ wfProfileIn( __METHOD__ );
+ global $wgLocalMessageCache, $wgLocalMessageCacheSerialized;
+
+ $success = $this->mMemc->add( $cacheKey.'-status', "loading", MSG_LOAD_TIMEOUT );
+ if ( !$success ) return true; # Other process should be updating them now
+
+ $i = 0;
+ if ( $memc ) {
+ # Save in memcached
+ # Keep trying if it fails, this is kind of important
+
+ for ($i=0; $i<20 &&
+ !$this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
+ $i++ ) {
+ usleep(mt_rand(500000,1500000));
+ }
+ }
+
+ # Save to local cache
+ if ( $wgLocalMessageCache !== false ) {
+ $serialized = serialize( $cache );
+ $hash = md5( $serialized );
+ $this->mMemc->set( "$cacheKey-hash", $hash, $this->mExpiry );
+ if ($wgLocalMessageCacheSerialized) {
+ $this->saveToLocal( $serialized, $hash, $code );
+ } else {
+ $this->saveToScript( $cache, $hash, $code );
+ }
+ }
+
+ if ( $i == 20 ) {
+ $this->mMemc->set( $cacheKey.'-status', 'error', 60*5 );
+ wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
+ $success = false;
+ } else {
+ $this->mMemc->delete( $cacheKey.'-status' );
+ $success = true;
+ }
+ wfProfileOut( __METHOD__ );
+ return $success;
+ }
+