Create cache for Markdown content. #51

This commit is contained in:
Stéphane Goetz 2018-06-06 23:20:29 +02:00
parent 4325008a5f
commit 47a9565b23
4 changed files with 146 additions and 2 deletions

95
libs/Cache.php Normal file
View File

@ -0,0 +1,95 @@
<?php namespace Todaymade\Daux;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux;
class Cache {
static $printed = false;
protected static function getDirectory()
{
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "dauxio" . DIRECTORY_SEPARATOR;
if (!Cache::$printed) {
Cache::$printed = true;
Daux::writeln("Using cache dir '$dir'", OutputInterface::VERBOSITY_VERBOSE);
}
return $dir;
}
/**
* Store an item in the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function put($key, $value)
{
Cache::ensureCacheDirectoryExists($path = Cache::path($key));
file_put_contents($path, serialize($value));
}
/**
* Create the file cache directory if necessary.
*
* @param string $path
* @return void
*/
protected static function ensureCacheDirectoryExists($path)
{
$parent = dirname($path);
if (!file_exists($parent)) {
mkdir($parent, 0777, true);
}
}
/**
* Remove an item from the cache.
*
* @param string $key
* @return bool
*/
public static function forget($key)
{
$path = Cache::path($key);
if (file_exists($path)) {
return unlink($path);
}
return false;
}
/**
* Retrieve an item from the cache by key.
*
* @param string|array $key
* @return mixed
*/
public static function get($key)
{
$path = Cache::path($key);
if (file_exists($path)) {
return file_get_contents($path);
}
return null;
}
/**
* Get the full path for the given cache key.
*
* @param string $key
* @return string
*/
protected static function path($key)
{
$parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);
return Cache::getDirectory().'/'.implode('/', $parts).'/'.$hash;
}
}

View File

@ -120,4 +120,23 @@ class Config extends BaseConfig
{
return new HTMLConfig($this['html']);
}
public function canCache()
{
if (array_key_exists('cache', $this)) {
return $this['cache'];
}
return true;
}
public function getCacheKey()
{
$cloned = [];
foreach ($this as $key => $value) {
$cloned[$key] = ($value instanceof Entry) ? $value->dump() : $value;
}
return sha1(json_encode($cloned));
}
}

View File

@ -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;
}
}

View File

@ -1,6 +1,9 @@
<?php namespace Todaymade\Daux\ContentTypes\Markdown;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Cache;
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Content;
class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
@ -25,10 +28,32 @@ class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
return ['md', 'markdown'];
}
protected function doConversion($raw)
{
Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE);
return $this->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;
}
}