Use the shared smarty cache instead of handling it manually
Use the shared smarty cache instead of handling it manually

This cuts down the code and allows us to use memcache if configured.
Since the list of projects is background-level data (and not HTML
pages), it uses the object cache. But since it's not immutable like git
objects, the lifetime uses the regular cache lifetime setting, which is
generally set shorter than the object cache lifetime (that could even be
set to -1).

--- a/include/cache/Cache.class.php
+++ b/include/cache/Cache.class.php
@@ -280,7 +280,9 @@
 		if ($this->tpl)
 			return;
 
+		require_once(GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('smarty_prefix', 'lib/smarty/libs/')) . 'Smarty.class.php');
 		$this->tpl = new Smarty;
+		$this->tpl->plugins_dir[] = GITPHP_INCLUDEDIR . 'smartyplugins';
 
 		$this->tpl->caching = 2;
 

--- a/include/git/ProjectListDirectory.class.php
+++ b/include/git/ProjectListDirectory.class.php
@@ -61,28 +61,17 @@
 	 */
 	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;
-				}
-
-			}
+		$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;
+			return;
 		}
 
 		$this->RecurseDir($this->projectDir);
-		$this->CacheSaveProjectList();
+
+		GitPHP_Cache::GetObjectCacheInstance()->Set($key, $this->projects, GitPHP_Config::GetInstance()->GetValue('cachelifetime', 3600));
 	}
 
 	/**
@@ -127,12 +116,5 @@
 		}
 	}
 
-	/** 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