diff --git a/libs/Cache.php b/libs/Cache.php new file mode 100644 index 0000000..954ba95 --- /dev/null +++ b/libs/Cache.php @@ -0,0 +1,95 @@ + $value) { + $cloned[$key] = ($value instanceof Entry) ? $value->dump() : $value; + } + + return sha1(json_encode($cloned)); + } } diff --git a/libs/Console/DauxCommand.php b/libs/Console/DauxCommand.php index dafa5ea..b207e07 100644 --- a/libs/Console/DauxCommand.php +++ b/libs/Console/DauxCommand.php @@ -14,7 +14,8 @@ class DauxCommand extends SymfonyCommand ->addOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Configuration file') ->addOption('value', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Set different configuration values') ->addOption('source', 's', InputOption::VALUE_REQUIRED, 'Where to take the documentation from') - ->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree'); + ->addOption('processor', 'p', InputOption::VALUE_REQUIRED, 'Manipulations on the tree') + ->addOption('no-cache', null, InputOption::VALUE_NONE, 'Disable Cache'); // HTML Format only $this->addOption('themes', 't', InputOption::VALUE_REQUIRED, 'Set a different themes directory'); @@ -79,6 +80,10 @@ class DauxCommand extends SymfonyCommand $this->applyConfiguration($input->getOption('value'), $daux); } + if ($input->getOption('no-cache')) { + $daux->getParams()['cache'] = false; + } + return $daux; } } diff --git a/libs/ContentTypes/Markdown/ContentType.php b/libs/ContentTypes/Markdown/ContentType.php index 01ebf9e..c1724de 100644 --- a/libs/ContentTypes/Markdown/ContentType.php +++ b/libs/ContentTypes/Markdown/ContentType.php @@ -1,6 +1,9 @@ converter->convertToHtml($raw); + } + public function convert($raw, Content $node) { $this->config->setCurrentPage($node); - return $this->converter->convertToHtml($raw); + if (!$this->config->canCache()) { + return $this->doConversion($raw); + } + + $cacheKey = $this->config->getCacheKey() . sha1($raw); + + $payload = Cache::get($cacheKey); + + if ($payload) { + Daux::writeln("Using cached version", OutputInterface::VERBOSITY_VERBOSE); + } else { + Daux::writeln("Not found in the cache, generating...", OutputInterface::VERBOSITY_VERBOSE); + $payload = $this->doConversion($raw); + Cache::put($cacheKey, $payload); + } + + return $payload; } }