Merge cached project directory code into ProjectListDirectory
Merge cached project directory code into ProjectListDirectory

Since cache handling is a logical extension of the directory lister
driver, it should be in the same class and handled internally. Also
this keeps caching code out of the ProjectList factory

--- a/include/git/ProjectList.class.php
+++ b/include/git/ProjectList.class.php
@@ -11,7 +11,6 @@
  */
 
 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');
@@ -88,15 +87,7 @@
 
 		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();
-			}
+			self::$instance = new GitPHP_ProjectListDirectory(GitPHP_Config::GetInstance()->GetValue('projectroot'));
 		}
 
 		if (isset($git_projects_settings) && !$legacy)

--- a/include/git/ProjectListBase.class.php
+++ b/include/git/ProjectListBase.class.php
@@ -320,12 +320,5 @@
 		$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);
-	}
 }
 

--- a/include/git/ProjectListDirCached.class.php
+++ /dev/null
@@ -1,66 +1,1 @@
-<?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);

-		}

-	}

-}

 

--- a/include/git/ProjectListDirectory.class.php
+++ b/include/git/ProjectListDirectory.class.php
@@ -61,7 +61,28 @@
 	 */
 	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...');
+			} else {
+
+				$data = file_get_contents(GITPHP_CACHE.'ProjectList.dat');
+				$projects = unserialize($data);
+				if (count($projects) > 0) {
+					GitPHP_Log::GetInstance()->Log('loaded '.count($projects).' projects from cache');
+					$this->projects = $projects;
+					return;
+				}
+
+			}
+		}
+
 		$this->RecurseDir($this->projectDir);
+		$this->CacheSaveProjectList();
 	}
 
 	/**
@@ -106,5 +127,12 @@
 		}
 	}
 
+	/** Save and restore project list to prevent parsing directories
+	 */
+	private function CacheSaveProjectList()
+	{
+		$data = serialize($this->projects);
+		return (file_put_contents(GITPHP_CACHE.'ProjectList.dat',$data) > 0);
+	}
 }
 

comments