diff --git a/manual/new/Cache.php b/manual/new/Cache.php index 7f6c96465..86c3fe28a 100644 --- a/manual/new/Cache.php +++ b/manual/new/Cache.php @@ -6,13 +6,11 @@ class Cache protected $_ext; protected $_page; protected $_file; - protected $_timeToLive; - public function __construct($dir, $ext, $timeToLive) + public function __construct($dir, $ext) { $this->_dir = $dir; $this->_ext = $ext; - $this->_timeToLive = $timeToLive; $this->_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $this->_file = $this->_dir . md5($this->_page) . '.' . $this->_ext; @@ -59,10 +57,10 @@ class Cache */ public function clear() { - if ($handle = opendir($this->dir)) { + if ($handle = opendir($this->_dir)) { while ($file = readdir($handle)) { if ($file !== '.' && $file !== '..') { - unlink($this->dir . '/' . $file); + @unlink($this->_dir . '/' . $file); } } closedir($handle); @@ -72,13 +70,12 @@ class Cache /** * This method is used to check whether the cache file is valid to use. * - * Currently it compares the modification date of the cache file to the - * time-to-live value. + * Currently it assumes that the cache file is always valid. * * @return True, if cache file is valid; false otherwise. */ protected function isValid() { - return (time() - filemtime($this->_file)) < $this->_timeToLive; + return true; } } \ No newline at end of file diff --git a/manual/new/index.php b/manual/new/index.php index 3793bc2f3..59b253b15 100644 --- a/manual/new/index.php +++ b/manual/new/index.php @@ -11,18 +11,35 @@ require_once('Cache.php'); spl_autoload_register(array('Sensei', 'autoload')); -// Executes the svn info command for the current directory and parses the last -// changed date in order to calculate the time-to-live value for cache. -$timeToLive = 0; +// Executes the 'svn info' command for the current directory and parses the last +// changed revision. +$revision = 0; exec('svn info .', $output); foreach ($output as $line) { - if (preg_match('/^Last Changed Date: (.*) \(.*\)$/', $line, $matches)) { - $timeToLive = time() - strtotime($matches[1]); + if (preg_match('/^Last Changed Rev: ([0-9]+)$/', $line, $matches)) { + $revision = $matches[1]; break; } } -$cache = new Cache('./cache/', 'cache', $timeToLive); +$cacheDir = './cache/'; +$cacheRevFile = $cacheDir . 'revision.txt'; +$cacheRev = 0; + +$cache = new Cache($cacheDir, 'cache'); + +// Checks the revision cache files were created from +if (file_exists($cacheRevFile)) { + $cacheRev = (int) file_get_contents($cacheRevFile); +} + +// Empties the cache directory and saves the current revision to a file, if SVN +// revision is greater than cache revision +if ($revision > $cacheRev) { + $cache->clear(); + @file_put_contents($cacheRevFile, $revision); +} + if ($cache->begin()) {