Don't cache entire project object
Don't cache entire project object

Serialization works recursively, which means everything contained inside
the project object is also serialized, which is a bunch of wasted space
and may not be safe.
The only thing we need to find a project is its path.

--- a/include/git/ProjectListDirectory.class.php
+++ b/include/git/ProjectListDirectory.class.php
@@ -64,14 +64,22 @@
 		$key = 'projectdir|' . $this->projectDir . '|projectlist|directory';
 		$cached = GitPHP_Cache::GetObjectCacheInstance()->Get($key);
 		if ($cached && (count($cached) > 0)) {
-			GitPHP_Log::GetInstance()->Log('Loaded ' . count($cached) . ' projects from cache');
-			$this->projects = $cached;
+			foreach ($cached as $proj) {
+				$this->AddProject($proj);
+			}
+			GitPHP_Log::GetInstance()->Log('Loaded ' . count($this->projects) . ' projects from cache');
 			return;
 		}
 
 		$this->RecurseDir($this->projectDir);
 
-		GitPHP_Cache::GetObjectCacheInstance()->Set($key, $this->projects, GitPHP_Config::GetInstance()->GetValue('cachelifetime', 3600));
+		if (count($this->projects) > 0) {
+			$projects = array();
+			foreach ($this->projects as $proj) {
+				$projects[] = $proj->GetProject();;
+			}
+			GitPHP_Cache::GetObjectCacheInstance()->Set($key, $projects, GitPHP_Config::GetInstance()->GetValue('cachelifetime', 3600));
+		}
 	}
 
 	/**
@@ -96,15 +104,7 @@
 					if (is_file($fullPath . '/HEAD')) {
 						GitPHP_Log::GetInstance()->Log(sprintf('Found project %1$s', $fullPath));
 						$projectPath = substr($fullPath, $trimlen);
-						try {
-							$proj = new GitPHP_Project($this->projectDir, $projectPath);
-							$proj->SetCategory(trim(substr($dir, strlen($this->projectDir)), '/'));
-							if ((!GitPHP_Config::GetInstance()->GetValue('exportedonly', false)) || $proj->GetDaemonEnabled()) {
-								$this->projects[$projectPath] = $proj;
-							}
-						} catch (Exception $e) {
-							GitPHP_Log::GetInstance()->Log($e->getMessage());
-						}
+						$this->AddProject($projectPath);
 					} else {
 						$this->RecurseDir($fullPath);
 					}
@@ -116,5 +116,28 @@
 		}
 	}
 
+	/**
+	 * AddProject
+	 *
+	 * Add project to collection
+	 *
+	 * @access private
+	 */
+	private function AddProject($projectPath)
+	{
+		try {
+			$proj = new GitPHP_Project($this->projectDir, $projectPath);
+			$category = trim(dirname($projectPath));
+			if (!(empty($category) || (strpos($category, '.') === 0))) {
+				$proj->SetCategory($category);
+			}
+			if ((!GitPHP_Config::GetInstance()->GetValue('exportedonly', false)) || $proj->GetDaemonEnabled()) {
+				$this->projects[$projectPath] = $proj;
+			}
+		} catch (Exception $e) {
+			GitPHP_Log::GetInstance()->Log($e->getMessage());
+		}
+	}
+
 }
 

comments