Migrate more callers away from $wgMemc
[lhc/web/wiklou.git] / includes / cache / MessageBlobStore.php
index 011cae6..b7c70c1 100644 (file)
  * constituent messages or the resource itself is changed.
  */
 class MessageBlobStore {
+       /**
+        * In-process cache for message blobs.
+        *
+        * Keyed by language code, then module name.
+        *
+        * @var array
+        */
+       protected $blobCache = array();
+
        /**
         * Get the singleton instance
         *
@@ -56,18 +65,40 @@ class MessageBlobStore {
                if ( !count( $modules ) ) {
                        return array();
                }
-               // Try getting from the DB first
-               $blobs = $this->getFromDB( $resourceLoader, array_keys( $modules ), $lang );
 
-               // Generate blobs for any missing modules and store them in the DB
-               $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
-               foreach ( $missing as $name ) {
+               $blobs = array();
+
+               // Try in-process cache
+               $missingFromCache = array();
+               foreach ( $modules as $name => $module ) {
+                       if ( isset( $this->blobCache[$lang][$name] ) ) {
+                               $blobs[$name] = $this->blobCache[$lang][$name];
+                       } else {
+                               $missingFromCache[] = $name;
+                       }
+               }
+
+               // Try DB cache
+               if ( $missingFromCache ) {
+                       $blobs += $this->getFromDB( $resourceLoader, $missingFromCache, $lang );
+               }
+
+               // Generate new blobs for any remaining modules and store in DB
+               $missingFromDb = array_diff( array_keys( $modules ), array_keys( $blobs ) );
+               foreach ( $missingFromDb as $name ) {
                        $blob = $this->insertMessageBlob( $name, $modules[$name], $lang );
                        if ( $blob ) {
                                $blobs[$name] = $blob;
                        }
                }
 
+               // Update in-process cache
+               if ( isset( $this->blobCache[$lang] ) ) {
+                       $this->blobCache[$lang] += $blobs;
+               } else {
+                       $this->blobCache[$lang] = $blobs;
+               }
+
                return $blobs;
        }
 
@@ -136,7 +167,7 @@ class MessageBlobStore {
         * @param string $name Module name
         * @param ResourceLoaderModule $module
         * @param string $lang Language code
-        * @return string Regenerated message blob, or null if there was no blob for
+        * @return string|null Regenerated message blob, or null if there was no blob for
         *   the given module/language pair.
         */
        public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
@@ -339,7 +370,10 @@ class MessageBlobStore {
         * @return array Array mapping module names to blobs
         */
        private function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) {
-               $config = $resourceLoader->getConfig();
+               if ( !count( $modules ) ) {
+                       return array();
+               }
+
                $retval = array();
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select( 'msg_resource',
@@ -355,13 +389,10 @@ class MessageBlobStore {
                                throw new MWException( __METHOD__ . ' passed an invalid module name' );
                        }
 
-                       // Update the module's blobs if the set of messages changed or if the blob is
-                       // older than the CacheEpoch setting
-                       $keys = array_keys( FormatJson::decode( $row->mr_blob, true ) );
-                       $values = array_values( array_unique( $module->getMessages() ) );
-                       if ( $keys !== $values
-                               || wfTimestamp( TS_MW, $row->mr_timestamp ) <= $config->get( 'CacheEpoch' )
-                       ) {
+                       // Update the module's blob if the list of messages changed
+                       $blobKeys = array_keys( FormatJson::decode( $row->mr_blob, true ) );
+                       $moduleMsgs = array_values( array_unique( $module->getMessages() ) );
+                       if ( $blobKeys !== $moduleMsgs ) {
                                $retval[$row->mr_resource] = $this->updateModule( $row->mr_resource, $module, $lang );
                        } else {
                                $retval[$row->mr_resource] = $row->mr_blob;