MDL-21208 major perf improvement for theme designer mode - now partially cached too ;-)

This commit is contained in:
Petr Skoda 2009-12-29 12:04:31 +00:00
parent 13ea159e9c
commit 7829cf7955
2 changed files with 38 additions and 21 deletions

View file

@ -587,8 +587,29 @@ class theme_config {
return array(new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params)); return array(new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params));
} else { } else {
// this is painfully slow, but it should not matter much ;-) // find out the current CSS and cache it now for 5 seconds
$css = $this->css_content(); // the point is to construct the CSS only once and pass it through the
// dataroot to the script that actually serves the sheets
$candidatesheet = "$CFG->dataroot/cache/theme/$this->name/designer.ser";
if (!file_exists($candidatesheet)) {
$css = $this->css_content();
check_dir_exists(dirname($candidatesheet), true, true);
file_put_contents($candidatesheet, serialize($css));
} else if (filemtime($candidatesheet) > time() - 5) {
if ($css = file_get_contents($candidatesheet)) {
$css = unserialize($css);
} else {
unlink($candidatesheet);
$css = $this->css_content();
}
} else {
unlink($candidatesheet);
$css = $this->css_content();
file_put_contents($candidatesheet, serialize($css));
}
$url = $CFG->httpswwwroot.'/theme/styles_debug.php'; $url = $CFG->httpswwwroot.'/theme/styles_debug.php';
$urls = array(); $urls = array();
$urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'yui2')); $urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'yui2'));

View file

@ -24,15 +24,13 @@
*/ */
// no chaching define('ABORT_AFTER_CONFIG', true);
define('NO_MOODLE_COOKIES', true); // Session not used here require('../config.php'); // this stops immediately at the beginning of lib/setup.php
define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
require('../config.php');
$themename = required_param('theme', PARAM_SAFEDIR); $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
$type = required_param('type', PARAM_SAFEDIR); $type = min_optional_param('type', 'all', 'SAFEDIR');
$subtype = optional_param('subtype', '', PARAM_SAFEDIR); $subtype = min_optional_param('subtype', '', 'SAFEDIR');
$sheet = optional_param('sheet', '', PARAM_SAFEDIR); $sheet = min_optional_param('sheet', '', 'SAFEDIR');
if (file_exists("$CFG->dirroot/theme/$themename/config.php")) { if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
// exists // exists
@ -42,19 +40,17 @@ if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
css_not_found(); css_not_found();
} }
if (theme_get_revision() > -1) { $candidatesheet = "$CFG->dataroot/cache/theme/$themename/designer.ser";
//bad luck - this should not happen!
if (!file_exists($candidatesheet)) {
css_not_found(); css_not_found();
} }
$theme = theme_config::load($themename); if (!$css = file_get_contents($candidatesheet)) {
css_not_found();
if ($type === 'editor') {
$css = $theme->editor_css_content();
send_uncached_css($css);
} }
$css = $theme->css_content(); $css = unserialize($css);
if ($type === 'yui2') { if ($type === 'yui2') {
send_uncached_css(reset($css['yui2'])); send_uncached_css(reset($css['yui2']));
@ -66,7 +62,7 @@ if ($type === 'yui2') {
} else if ($type === 'parent') { } else if ($type === 'parent') {
if (isset($css['parents'][$subtype][$sheet])) { if (isset($css['parents'][$subtype][$sheet])) {
send_uncached_css($css['parents'][$subtype][$sheet]); send_uncached_css($css['parents'][$subtype][$sheet], 30); // parent sheets are not supposed to change much, right?
} }
} else if ($type === 'theme') { } else if ($type === 'theme') {
@ -81,10 +77,10 @@ css_not_found();
// we are not using filelib because we need to fine tune all header // we are not using filelib because we need to fine tune all header
// parameters to get the best performance. // parameters to get the best performance.
function send_uncached_css($css) { function send_uncached_css($css, $lifetime = 2) {
header('Content-Disposition: inline; filename="styles_debug.php"'); header('Content-Disposition: inline; filename="styles_debug.php"');
header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT'); header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
header('Pragma: '); header('Pragma: ');
header('Accept-Ranges: none'); header('Accept-Ranges: none');
header('Content-Type: text/css'); header('Content-Type: text/css');