MDL-33430 Add caching into choose_repository() method
authorDavid Mudrák <david@moodle.com>
Thu, 21 Jun 2012 10:15:48 +0000 (12:15 +0200)
committerDavid Mudrák <david@moodle.com>
Thu, 21 Jun 2012 10:17:54 +0000 (12:17 +0200)
Simply do not repeat expensive search once the restore process decides how
to deal with the given repositoryid or repositorytype.

backup/moodle2/restore_stepslib.php

index 6178c29..503521c 100644 (file)
@@ -3110,6 +3110,12 @@ class restore_create_question_files extends restore_execution_step {
  */
 class restore_process_file_aliases_queue extends restore_execution_step {
 
+    /** @var array internal cache for {@link choose_repository() */
+    private $cachereposbyid = array();
+
+    /** @var array internal cache for {@link choose_repository() */
+    private $cachereposbytype = array();
+
     /**
      * What to do when this step is executed.
      */
@@ -3270,18 +3276,34 @@ class restore_process_file_aliases_queue extends restore_execution_step {
 
         if ($this->task->is_samesite()) {
             // We can rely on repository instance id.
+
+            if (array_key_exists($info->oldfile->repositoryid, $this->cachereposbyid)) {
+                return $this->cachereposbyid[$info->oldfile->repositoryid];
+            }
+
+            $this->log('looking for repository instance by id', backup::LOG_DEBUG, $info->oldfile->repositoryid, 1);
+
             try {
-                return repository::get_repository_by_id($info->oldfile->repositoryid, SYSCONTEXTID);
+                $this->cachereposbyid[$info->oldfile->repositoryid] = repository::get_repository_by_id($info->oldfile->repositoryid, SYSCONTEXTID);
+                return $this->cachereposbyid[$info->oldfile->repositoryid];
             } catch (Exception $e) {
+                $this->cachereposbyid[$info->oldfile->repositoryid] = null;
                 return null;
             }
 
         } else {
             // We can rely on repository type only.
+
             if (empty($info->oldfile->repositorytype)) {
                 return null;
             }
 
+            if (array_key_exists($info->oldfile->repositorytype, $this->cachereposbytype)) {
+                return $this->cachereposbytype[$info->oldfile->repositorytype];
+            }
+
+            $this->log('looking for repository instance by type', backup::LOG_DEBUG, $info->oldfile->repositorytype, 1);
+
             // Both Server files and Legacy course files repositories have a single
             // instance at the system context to use. Let us try to find it.
             if ($info->oldfile->repositorytype === 'local' or $info->oldfile->repositorytype === 'coursefiles') {
@@ -3295,12 +3317,15 @@ class restore_process_file_aliases_queue extends restore_execution_step {
                 }
                 $repoid = reset(array_keys($ris));
                 try {
-                    return repository::get_repository_by_id($repoid, SYSCONTEXTID);
+                    $this->cachereposbytype[$info->oldfile->repositorytype] = repository::get_repository_by_id($repoid, SYSCONTEXTID);
+                    return $this->cachereposbytype[$info->oldfile->repositorytype];
                 } catch (Exception $e) {
+                    $this->cachereposbytype[$info->oldfile->repositorytype] = null;
                     return null;
                 }
             }
 
+            $this->cachereposbytype[$info->oldfile->repositorytype] = null;
             return null;
         }
     }