Commit 5fefbbd8 authored by jepso's avatar jepso

Documentation cache uses 'Last Changed Rev' instead of 'Last Changed Date' now.

parent 4cb332fb
...@@ -6,13 +6,11 @@ class Cache ...@@ -6,13 +6,11 @@ class Cache
protected $_ext; protected $_ext;
protected $_page; protected $_page;
protected $_file; protected $_file;
protected $_timeToLive;
public function __construct($dir, $ext, $timeToLive) public function __construct($dir, $ext)
{ {
$this->_dir = $dir; $this->_dir = $dir;
$this->_ext = $ext; $this->_ext = $ext;
$this->_timeToLive = $timeToLive;
$this->_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $this->_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$this->_file = $this->_dir . md5($this->_page) . '.' . $this->_ext; $this->_file = $this->_dir . md5($this->_page) . '.' . $this->_ext;
...@@ -59,10 +57,10 @@ class Cache ...@@ -59,10 +57,10 @@ class Cache
*/ */
public function clear() public function clear()
{ {
if ($handle = opendir($this->dir)) { if ($handle = opendir($this->_dir)) {
while ($file = readdir($handle)) { while ($file = readdir($handle)) {
if ($file !== '.' && $file !== '..') { if ($file !== '.' && $file !== '..') {
unlink($this->dir . '/' . $file); @unlink($this->_dir . '/' . $file);
} }
} }
closedir($handle); closedir($handle);
...@@ -72,13 +70,12 @@ class Cache ...@@ -72,13 +70,12 @@ class Cache
/** /**
* This method is used to check whether the cache file is valid to use. * 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 * Currently it assumes that the cache file is always valid.
* time-to-live value.
* *
* @return True, if cache file is valid; false otherwise. * @return True, if cache file is valid; false otherwise.
*/ */
protected function isValid() protected function isValid()
{ {
return (time() - filemtime($this->_file)) < $this->_timeToLive; return true;
} }
} }
\ No newline at end of file
...@@ -11,18 +11,35 @@ require_once('Cache.php'); ...@@ -11,18 +11,35 @@ require_once('Cache.php');
spl_autoload_register(array('Sensei', 'autoload')); spl_autoload_register(array('Sensei', 'autoload'));
// Executes the svn info command for the current directory and parses the last // 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. // changed revision.
$timeToLive = 0; $revision = 0;
exec('svn info .', $output); exec('svn info .', $output);
foreach ($output as $line) { foreach ($output as $line) {
if (preg_match('/^Last Changed Date: (.*) \(.*\)$/', $line, $matches)) { if (preg_match('/^Last Changed Rev: ([0-9]+)$/', $line, $matches)) {
$timeToLive = time() - strtotime($matches[1]); $revision = $matches[1];
break; 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()) { if ($cache->begin()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment