From 32e70ed86acd49fafc4cb3d0632d58f1f1f26eaa Mon Sep 17 00:00:00 2001 From: Luqgreg Date: Wed, 15 Aug 2018 16:31:42 +0200 Subject: [PATCH] Cache compiled handlebars templates --- .../Helpers/PortableInfoboxTemplateEngine.php | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/includes/services/Helpers/PortableInfoboxTemplateEngine.php b/includes/services/Helpers/PortableInfoboxTemplateEngine.php index 27b9f39..21c5d79 100644 --- a/includes/services/Helpers/PortableInfoboxTemplateEngine.php +++ b/includes/services/Helpers/PortableInfoboxTemplateEngine.php @@ -5,9 +5,11 @@ namespace PortableInfobox\Helpers; use MediaWiki\Logger\LoggerFactory; class PortableInfoboxTemplateEngine { + const CACHE_TTL = 86400; const TYPE_NOT_SUPPORTED_MESSAGE = 'portable-infobox-render-not-supported-type'; private static $cache = []; + private static $memcache; protected static $templates = [ 'wrapper' => 'PortableInfoboxWrapper.hbs', @@ -26,7 +28,11 @@ class PortableInfoboxTemplateEngine { 'xml-parse-error' => 'PortableInfoboxMarkupDebug.hbs' ]; - public function __construct() {} + public function __construct() { + if( !isset( self::$memcache ) ) { + self::$memcache = \ObjectCache::getMainWANInstance(); + } + } public static function getTemplatesDir() { return dirname( __FILE__ ) . '/../../../templates'; @@ -52,13 +58,17 @@ class PortableInfoboxTemplateEngine { return self::$cache[ $type ]; } - $path = self::getTemplatesDir() . DIRECTORY_SEPARATOR . static::getTemplates()[ $type ]; + $cachekey = self::$memcache->makeKey( __CLASS__, \PortableInfoboxParserTagController::PARSER_TAG_VERSION, $type ); // @see https://github.com/wikimedia/mediawiki-vendor/tree/master/zordius/lightncandy $renderer = \LightnCandy::prepare( - \LightnCandy::compile( file_get_contents( $path ), [ - 'flags' => \LightnCandy::FLAG_BESTPERFORMANCE - ] ) + self::$memcache->getWithSetCallback( $cachekey, self::CACHE_TTL, function () use ( $type ) { + $path = self::getTemplatesDir() . DIRECTORY_SEPARATOR . static::getTemplates()[ $type ]; + + return \LightnCandy::compile( file_get_contents( $path ), [ + 'flags' => \LightnCandy::FLAG_BESTPERFORMANCE + ] ); + } ) ); self::$cache[ $type ] = $renderer;