Cache project list if set to auto discover them
Cache project list if set to auto discover them
(cherry picked from commit 7b5bc0db7ec4ac45a02535c099530bd7a280ca86)

Conflicts:

include/git/ProjectListDirectory.class.php

--- a/include/git/ProjectList.class.php
+++ b/include/git/ProjectList.class.php
@@ -11,6 +11,7 @@
  */
 
 require_once(GITPHP_GITOBJECTDIR . 'ProjectListDirectory.class.php');
+require_once(GITPHP_GITOBJECTDIR . 'ProjectListDirCached.class.php');
 require_once(GITPHP_GITOBJECTDIR . 'ProjectListFile.class.php');
 require_once(GITPHP_GITOBJECTDIR . 'ProjectListArray.class.php');
 require_once(GITPHP_GITOBJECTDIR . 'ProjectListArrayLegacy.class.php');
@@ -66,6 +67,7 @@
 		if (self::$instance)
 			return;
 
+
 		if (!empty($file) && is_file($file) && include($file)) {
 			if (isset($git_projects)) {
 				if (is_string($git_projects)) {
@@ -84,8 +86,18 @@
 			}
 		}
 
-		if (!self::$instance)
-			self::$instance = new GitPHP_ProjectListDirectory(GitPHP_Config::GetInstance()->GetValue('projectroot'));
+		if (!self::$instance) {
+
+			$cache = new GitPHP_ProjectListDirCached($git_projects);
+
+			if ($cache->Count()) {
+				GitPHP_Log::GetInstance()->Log('loaded '.$cache->Count().' projects from cache');
+				self::$instance = $cache;
+			} else {
+				self::$instance = new GitPHP_ProjectListDirectory(GitPHP_Config::GetInstance()->GetValue('projectroot'));
+				self::$instance->CacheSaveProjectList();
+			}
+		}
 
 		if (isset($git_projects_settings) && !$legacy)
 			self::$instance->ApplySettings($git_projects_settings);

--- a/include/git/ProjectListBase.class.php
+++ b/include/git/ProjectListBase.class.php
@@ -320,5 +320,12 @@
 		$this->projectSettings = $settings;
 	}
 
+	/** Save and restore project list to prevent parsing directories
+	 */
+	public function CacheSaveProjectList()
+	{
+		$data = serialize($this->projects);
+		return (file_put_contents(GITPHP_CACHE.'ProjectList.dat',$data) > 0);
+	}
 }
 

--- /dev/null
+++ b/include/git/ProjectListDirCached.class.php
@@ -1,1 +1,66 @@
+<?php

+/**

+ * GitPHP ProjectListDirCached

+ *

+ * Load projects from cache

+ *

+ * @author Tanguy Pruvot <tpruvot@github>

+ * @package GitPHP

+ * @subpackage Git

+ */

+

+require_once(GITPHP_INCLUDEDIR . 'Config.class.php');

+require_once(GITPHP_GITOBJECTDIR . 'ProjectListBase.class.php');

+require_once(GITPHP_GITOBJECTDIR . 'Project.class.php');

+

+/**

+ * ProjectListDirCached class

+ *

+ * @package GitPHP

+ * @subpackage Git

+ */

+class GitPHP_ProjectListDirCached extends GitPHP_ProjectListBase

+{

+	

+	/**

+	 * __construct

+	 *

+	 * constructor

+	 *

+	 * @access public

+	 */

+	public function __construct($projectArray)

+	{

+		parent::__construct();

+

+		$this->projectConfig = $projectArray;

+

+		$this->PopulateProjects();

+	}

+

+	/**

+	 * PopulateProjects

+	 *

+	 * Populates the internal list of projects

+	 *

+	 * @access protected

+	 * @throws Exception if file cannot be read

+	 */

+	protected function PopulateProjects()

+	{

+		$stat = stat(GITPHP_CACHE.'ProjectList.dat');

+		if ($stat !== FALSE) {

+			$cache_life = '180';  //caching time, in seconds

+			$filemtime = max($stat['mtime'], $stat['ctime']);

+			

+			if  (time() - $filemtime >= $cache_life) {

+				GitPHP_Log::GetInstance()->Log('ProjectListDirCache: expired, reloading...');

+				return;

+			}

+

+			$data = file_get_contents(GITPHP_CACHE.'ProjectList.dat');

+			$this->projects = unserialize($data);

+		}

+	}

+}

 

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -25,6 +25,8 @@
 define('GITPHP_CONTROLLERDIR', GITPHP_INCLUDEDIR . 'controller/');
 define('GITPHP_CACHEDIR', GITPHP_INCLUDEDIR . 'cache/');
 define('GITPHP_LOCALEDIR', GITPHP_BASEDIR . 'locale/');
+
+define('GITPHP_CACHE', GITPHP_BASEDIR . 'cache/');
 
 include_once(GITPHP_INCLUDEDIR . 'version.php');
 

comments