From dc2928a14dcd29bf8502b6a9dc1357ad0e624251 Mon Sep 17 00:00:00 2001 From: czaks Date: Sun, 5 Apr 2015 18:48:53 +0200 Subject: [PATCH] cache_config preliminary release --- inc/config.php | 5 ++++ inc/functions.php | 73 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/inc/config.php b/inc/config.php index be1bb8e1..3a63054c 100644 --- a/inc/config.php +++ b/inc/config.php @@ -137,6 +137,11 @@ // Tinyboard to use. $config['cache']['redis'] = array('localhost', 6379, '', 1); + // EXPERIMENTAL: Should we cache configs? Warning: this changes board behaviour, i'd say, a lot. + // If you have any lambdas/includes present in your config, you should move them to instance-functions.php + // (this file will be explicitly loaded during cache hit, but not during cache miss). + $config['cache_config'] = false; + /* * ==================== * Cookie settings diff --git a/inc/functions.php b/inc/functions.php index fc22e6ce..7b4dde7e 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -50,15 +50,40 @@ $current_locale = 'en'; function loadConfig() { - global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale; + global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale, $events; $error = function_exists('error') ? 'error' : 'basic_error_function_because_the_other_isnt_loaded_yet'; - reset_events(); + $boardsuffix = isset($board['uri']) ? $board['uri'] : ''; if (!isset($_SERVER['REMOTE_ADDR'])) $_SERVER['REMOTE_ADDR'] = '0.0.0.0'; + if (file_exists('tmp/cache/cache_config.php')) { + require_once('tmp/cache/cache_config.php'); + } + + + if (isset($config['cache_config']) && + $config['cache_config'] && + $config = Cache::get('config_' . $boardsuffix ) ) { + $events = Cache::get('events_' . $boardsuffix ); + + if (file_exists('inc/instance-functions.php')) { + require_once('inc/instance-functions.php'); + } + + if ($config['locale'] != $current_locale) { + $current_locale = $config['locale']; + init_locale($config['locale'], $error); + } + } + else { + $config = array(); + // We will indent that later. + + reset_events(); + $arrays = array( 'db', 'api', @@ -86,7 +111,6 @@ function loadConfig() { 'dashboard_links' ); - $config = array(); foreach ($arrays as $key) { $config[$key] = array(); } @@ -99,7 +123,7 @@ function loadConfig() { // Those calls are expensive. Unfortunately, our cache system is not initialized at this point. // So, we may store the locale in a tmp/ filesystem. - if (file_exists($fn = 'tmp/cache/locale_' . ( isset($board['uri']) ? $board['uri'] : '' ) ) ) { + if (file_exists($fn = 'tmp/cache/locale_' . $boardsuffix ) ) { $config['locale'] = file_get_contents($fn); } else { @@ -138,8 +162,6 @@ function loadConfig() { init_locale($config['locale'], $error); } - date_default_timezone_set($config['timezone']); - if (!isset($config['global_message'])) $config['global_message'] = false; @@ -219,8 +241,22 @@ function loadConfig() { if (!isset($config['user_flags'])) $config['user_flags'] = array(); + if (!isset($__version)) + $__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false; + $config['version'] = $__version; + + if ($config['allow_roll']) + event_handler('post', 'diceRoller'); + + if (is_array($config['anonymous'])) + $config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])]; + + + } // Effectful config processing below: + date_default_timezone_set($config['timezone']); + if ($config['root_file']) { chdir($config['root_file']); } @@ -233,10 +269,6 @@ function loadConfig() { if (preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m)) $_SERVER['REMOTE_ADDR'] = $m[2]; - if (!isset($__version)) - $__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false; - $config['version'] = $__version; - if ($config['verbose_errors']) { set_error_handler('verbose_error_handler'); error_reporting(E_ALL); @@ -260,13 +292,22 @@ function loadConfig() { event_handler('post', 'postHandler'); } - if (is_array($config['anonymous'])) - $config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])]; - - if ($config['allow_roll']) - event_handler('post', 'diceRoller'); - event('load-config'); + + if ($config['cache_config'] && !isset ($config['cache_config_loaded'])) { + file_put_contents('tmp/cache/cache_config.php', '